Date and Time in C ++
The C ++ Standard Library (C ++ Standard Library) does not provide an appropriate Date type. C ++ inherits the structure and function to manipulate Date and Time from C. To access functions and structures related to Date and Time, you will need to declare in your C ++ program.
There are 4 time-related types: clock_t, time_t, size_t, and tm . In which clock_t, size_t and time_t types can represent System date and time in int form.
The structure type tm holds the Date and Time in the form of a C structure with the following elements:
struct tm { int tm_sec ; // so giay trong mot phut, tu 0 toi 61 int tm_min ; // so phut trong mot gio, tu 0 toi 59 int tm_hour ; // so gio trong mot ngay, tu 0 toi 24 int tm_mday ; // ngay trong thang, tu 1 toi 31 int tm_mon ; // thang trong nam, tu 0 toi 11 int tm_year ; // nam, bat dau tu 1900 int tm_wday ; // ngay, bat dau tu Sunday int tm_yday ; // ngay, bat dau tu 1/1 int tm_isdst ; // so gio cua Daylight Savings Time }
The table below lists important functions while you work with Date and Time in C or C ++. All of this function is part of the standard C and C ++ library and you can check the details using the reference to the C ++ standard library provided below:
STTHàm & Purpose1 time_t time (time_t * time);Returns the scheduled time of the system, which is the number of seconds that have elapsed since January 1, 1970. If the system does not have time, it returns -1
2 char * ctime (const time_t * time);This function returns a pointer to a pattern string: day year year hours: minutes: seconds yearn .
3 struct tm * localtime (const time_t * time);struct tm * localtime (const time_t * time); This function returns a pointer to the tm structure representing local time
4 clock_t clock (void);This function returns a value that is approximately equal to the amount of time the calling program is running. Returns -1 if time is not available
5 char * asctime (const struct tm * time);Returns a pointer to a string that contains information stored in the structure pointed to by time converted to a template: day month date hours: minutes: seconds yearn
6 struct tm * gmtime (const time_t * time);Returns a pointer to time in the tm structure pattern. Time is represented by Coordinated Universal Time (UTC), essentially Greenwich Mean Time (GMT).
7 time_t mktime (struct tm * time);Returns the scheduled time equivalent to the time found in the structure pointed to by time
8 double difftime (time_t time2, time_t time1);This function calculates the difference in the number of seconds of time1 and time2
9 size_t strftime ();This function can be used to format the Date and Time in a specific format
Current Date and Time in C ++
Suppose you want to get the current date and time of the system: either local time or UTC format. The following example performs the above work:
#include #include using namespace std ; int main ( ) { // tra ve date/time hien tai dua tren system hien tai time_t hientai = time ( 0 ); // chuyen doi hientai thanh dang chuoi char * dt = ctime (& hientai ); cout << "Date va Time dang local la: " << dt << endl ; // chuyen doi hientai thanh dang tm struct cho UTC tm * gmtm = gmtime (& hientai ); dt = asctime ( gmtm ); cout << "UTC date va time la: " << dt << endl ; }
Compiling and running the above C ++ program will produce the following results:
Time format uses the struct struct in C ++
The struct tm structure is very important while working with Date and Time in C and C ++. This structure holds the Date and Time in the sample of a C structure mentioned above. Most time-related functions use tm structure. The following example uses various functions related to Date and Time and tm structure:
While using the structure in this chapter, I assume you have a basic understanding of the structure in C and how to access the members of the structure using the -> operator.
#include #include using namespace std ; int main ( ) { // tra ve date/time hien tai dua tren system hien tai time_t baygio = time ( 0 ); cout << "So giay ke tu 1/1/1970 la: " << baygio << endl ; tm * ltm = localtime (& baygio ); // in cac thanh phan cua cau truc tm struct. cout << "Nam: " << 1900 + ltm -> tm_year << endl ; cout << "Thang: " << 1 + ltm -> tm_mon << endl ; cout << "Ngay: " << ltm -> tm_mday << endl ; cout << "Thoi gian: " << 1 + ltm -> tm_hour << ":" ; cout << 1 + ltm -> tm_min << ":" ; cout << 1 + ltm -> tm_sec << endl ; }
Compiling and running the above C ++ program will produce the following results:
According to Tutorialspoint
Previous article: Reference in C ++
Next article: Input / Output in C ++
You should read it
- How to change time, date and month in Windows 10?
- How to change date and time on Windows 11
- How to change date and time manually in macOS
- time.h in C
- Date & Time in PHP
- Why must I set the correct date and time for Smart TV?
- Current date and time in Python
- How to Set the Time and Date on a Partner Phone System
- How to change the date and time format on Windows?
- Quickly insert current date and time into a cell in Excel
- Structure (Struct) in C #
- How to insert time, date and time in Word
Maybe you are interested
How to share YouTube Music over time
It's time to switch to Passkey: The anti-phishing password alternative!
How to talk to a girl you like for the first time
6 signs it's time to upgrade your Smart TV
5 most powerful stealth weapons of all time of America
[99% Fixed] Clock Watchdog Timeout Blue Screen Error