How to create date object in c++?

Member

by sallie , in category: General Help , a year ago

How to create date object in c++?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by aliya.yundt , a year ago

@sallie 

In C++, you can create a date object by using the built-in "struct tm" and "time_t" types provided in the standard library. Here's an example of how you can create a date object:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <ctime>

int main() {
    // Get the current time
    std::time_t currentTime = std::time(nullptr);

    // Convert the current time to a struct tm
    struct std::tm* currentDate = std::localtime(&currentTime);

    // Access individual components of the date
    int year = currentDate->tm_year + 1900;   // Year since 1900
    int month = currentDate->tm_mon + 1;       // Month (0-11, so add 1)
    int day = currentDate->tm_mday;            // Day of the month

    // Print the current date
    std::cout << "Current date: " << year << "-" << month << "-" << day << std::endl;

    return 0;
}


In the above example, we use the std::time function to get the current time as the number of seconds since the Unix epoch. Then, we pass this value to std::localtime to convert it to a struct tm object representing the current date and time in the local timezone. Finally, we access individual components of the date using the members of struct tm and print them.


Note that struct tm represents dates and times as local time, so if you need to work with dates and times in UTC or other timezones, you may need to use other functions from the <ctime> library or third-party libraries like Boost.Date_Time.

Member

by enrico , 8 months ago

@sallie 

In C++20, a new standard library module named "chrono" has been introduced to provide better support for date and time handling. You can use this module to create a date object more conveniently.


Here's an example of how to create a date object using the std::chrono library:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <chrono>

int main() {
    // Get the current system clock time
    auto currentTime = std::chrono::system_clock::now();

    // Convert the system clock time to a date
    std::time_t currentTime_t = std::chrono::system_clock::to_time_t(currentTime);

    // Convert the time_t to tm struct for local time conversion
    std::tm* currentDate = std::localtime(&currentTime_t);

    // Access individual components of the date
    int year = currentDate->tm_year + 1900;   // Year since 1900
    int month = currentDate->tm_mon + 1;      // Month (0-11, so add 1)
    int day = currentDate->tm_mday;           // Day of the month

    // Print the current date
    std::cout << "Current date: " << year << "-" << month << "-" << day << std::endl;

    return 0;
}


In this code snippet, we use std::chrono::system_clock::now() to get the current time, convert it to a time_t object using std::chrono::system_clock::to_time_t(), and then convert it to a tm struct using std::localtime(). Finally, we access and print the year, month, and day components of the current date.


Remember to compile with the C++20 standard enabled to use the std::chrono library.