Table of Contents
Date and Time in Cplusplus: Date and Time in C ++ is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
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 help 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 example below 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 ; }
Frequently Asked Questions
What should you know about 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 example below performs the above work:
What should you know about 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 example below uses various.
What is Date and Time in Cplusplus: Date and Time in C ++?
The C ++ Standard Library (C ++ Standard Library) does not provide an appropriate Date type.
Was this article helpful?
Your feedback helps us improve.
Reader Comments 0
Sign in with email or Google to join the discussion.