C asctime() function
C asctime() function - Convert date and time to a string
Syntax:
char *asctime(const struct tm *timeptr)
The asctime() function is used to convert time, stored as a structure pointed to by time, to a character string.
The fields of the tm structure include:
tm_sec | Seconds (0-61) |
tm_min | Minutes (0-59) |
tm_hour | Hours (0-23) |
tm_mday | Day of month (1-31) |
tm_mon | Month (0-11; January = 0) |
tm_year | Year (current year minus 1900) |
tm_wday | Day of week (0-6; Sunday = 0) |
tm_yday | Day of year (0-365; January 1 = 0) |
tm_isdst | Zero if daylight saving time is not in effect; positive if daylight saving time is in effect; negative if the information is not available. |
Parameters:
Name | Description | Required /Optional |
---|---|---|
timeptr | Time/date structure. | Required |
Return value from asctime()
- Upon successful completion, asctime() shall return a pointer to the string.
- If the function is unsuccessful, it shall return NULL.
Example: asctime() function
#include <time.h>
#include <stdio.h>
int main(void)
{
struct tm *new_time;
time_t lctime;
// Get the time in seconds
time(&lctime);
// Convert it to the structure tm
new_time = localtime(&lctime);
// Print the local time as a string
printf("The current date and time are: %s", asctime(new_time));
}
Output:
The current date and time are: Mon Dec 19 21:32:36 2022
C Programming Code Editor:
Previous C Programming: C time.h Home
Next C Programming: C clock()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming/time/c-asctime.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics