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 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
May be interested
- Instructions to hide date and time from Windows 11 Taskbarwindows 11 version 24h2 allows you to hide the date and time from the taskbar if users want to tidy up the screen interface.
- Quickly insert current date and time into a cell in Excelin excel, you can quickly fill out current date, month, and time information as static or dynamic values. the static value is the date and time value at the time of filling without automatically updating on subsequent days when you open or perform calculations on a spreadsheet.
- The date command in Windowsthe date command displays or sets the system date. if used without parameters, the date command displays the current system date setting and prompts you to enter a new date.
- The easiest way to adjust the date and time on Windows 11in most cases, windows will automatically synchronize the date and time, but for some reason this information is not displayed correctly on windows 11, you can change it manually, article below will share with you how to adjust the date and time on windows 11 with specific instructions.
- Fix computer error that does not automatically update standard date and timethe computer displaying the wrong time or not automatically updating the standard date and time will lead to the system not working properly, not being able to access the internet or use some applications, etc. at this time, it may be possible. follow the steps to fix the error of your computer not automatically updating the standard date and time to ensure your device displays the correct time, avoiding unnecessary troubles.
- How to manage date and time in React using Moment.jsmoment.js is a great library for efficient date and time management in react apps. here are detailed instructions.
- How to fix the date #VALUE error in Excelwhen entering a date-related formula in excel, some people are reported with the #value error, affecting the results in excel files.
- Date time functions in Excelexcel supports you to process and calculate quickly with the functions that excel provides such as calculation functions, date functions ... one of them is the function of time to help you handle the prices. time value: hour, minute, second conveniently
- How to automatically update the Excel file editing timewhen inserting and editing time excel file, you will control the latest time to edit the file if shared with many people.
- How to determine the posting date of a website or an information on the Internetwhen you do research on a topic or simply need to look up and synthesize certain information, it's important to make sure your source is up to date.