Example of std::chrono::day in C++

The following program illustrates how to use std::chrono::day in C++:

C++
// C++ Program for demonstrating the use of std::chrono::day
#include <chrono>
#include <ctime>
#include <iostream>

int main()
{
    using namespace std;
    using namespace std::chrono;

    // Get the current time as a time_point
    auto now = system_clock::now();
    auto today = floor<days>(now); // Truncate to days

    // Get the calendar date
    year_month_day ymd = year_month_day{ today };
    day d = ymd.day();

    // Output the day of the month
    cout << "Day of the month:-" << endl;
    cout << "Today: " << static_cast<unsigned>(d) << endl;
    d++;
    cout << "Tommorow: " << static_cast<unsigned>(d)
         << endl;

    return 0;
}


Output

Day of the month:-
Today: 4
Tommorow: 5

Time Complexity: O(1)
Auxiliary Space: O(1)

Explanation: The above program fetches the current date and extracts the day of the month from it using the std::chrono::day class.

Note: This program will only work in C++ 20 or above versions.


std::chrono::day in C++ 20

In C++, the <ctime> header provides the std::chrono::day class that represents a day in the given month. In this article, we will learn how to use the std::chrono::day class in C++.

Similar Reads

std::chrono::day in C++

The std::chrono::day class was introduced in C++ 20 to represent a day in a month. It is a duration-based representation of the day in the calendar month. It allows for the precise calculations and manipulations of the dates and time durations. It is particularly useful when dealing with calendar-related operations and when precise handling of the time intervals is required....

Example of std::chrono::day in C++

The following program illustrates how to use std::chrono::day in C++:...

Contact Us