How to obtain the current date in C language?
To obtain the current date in C language, you can use the functions in the time.h header file. The specific steps are as follows:
- Include the header file: #include
- Declare a variable of type time_t: time_t current_time;
- Assign the current time to the variable current_time using the time function: time(¤t_time);
- Convert a time_t type time to a tm structure type using the localtime function: “`struct tm *local_time = localtime(¤t_time);“`
- Get the current year, month, and day using the members tm_year, tm_mon, and tm_mday of the tm structure type:
int year = local_time->tm_year + 1900;
int month = local_time->tm_mon + 1;
int day = local_time->tm_mday;
By following the steps above, you can obtain the current year, month, and day information.