C++ <ctime> - asctime() Function
The C++ <ctime> 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 <iostream> #include <ctime> using namespace std; int main (){ time_t t = time(NULL); struct tm * timeinfo = localtime(&t); //displaying the result cout<<"Current local time & date: "<<asctime(timeinfo); return 0; }
The output of the above code will be:
Current local time & date: Fri Apr 16 13:00:20 2021
❮ C++ <ctime> Library