C <time.h> - asctime() Function
The C <time.h> asctime() function converts the contents of the tm structure pointed by timeptr into calendar time and then to a textual representation. The returned string has the following format:
Www Mmm dd hh:mm:ss yyyy
- Www - Www - the day of the week from timeptr->tm_wday (one of Mon, Tue, Wed, Thu, Fri, Sat, Sun)
- Mmm - the month from timeptr->tm_mon (one of Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
- dd - the day of the month from timeptr->tm_mday as if printed by sprintf using %2d
- hh - hours from timeptr->tm_hour as if printed by sprintf using %.2d
- mm - minutes from timeptr->tm_min as if printed by sprintf using %.2d
- ss - seconds from timeptr->tm_sec as if printed by sprintf using %.2d
- yyyy - years from timeptr->tm_year + 1900 as if printed by sprintf using %4d
The string is followed by a new-line character ('\n') and terminated with a null-character. The behavior is undefined if any member of *timeptr is outside its normal range.
Syntax
char* asctime (const struct tm * timeptr);
Parameters
timeptr |
Specify pointer to a tm object specifying the time to print. |
Return Value
Returns a C-string containing textual representation of date and time. The string may be shared between asctime() and ctime(), and may be overwritten on each invocation of any of these functions.
Example:
The example below shows the usage of asctime() function.
#include <stdio.h> #include <time.h> int main (){ time_t t = time(NULL); struct tm * timeinfo = localtime(&t); //displaying the result printf("Current local time & date: %s", asctime(timeinfo)); return 0; }
The output of the above code will be:
Current local time & date: Fri Apr 16 13:07:33 2021
❮ C <time.h> Library