C++ ctime asctime() Function
Example
Display the date and time from a tm
structure:
time_t timestamp;
time(×tamp);
struct tm * myTime = localtime(×tamp);
cout << asctime(myTime);
Try it Yourself »
Definition and Usage
The asctime()
function returns a pointer to a C-style string representing the date and time of a tm
structure. The returned string has the format WWW MMM DD HH:mm:ss YYYY
(for example: "Sun Dec 17 21:34:26 2023"). For more control over date and time formatting, see the strftime() function.
The asctime()
function is defined in the <ctime>
header file.
Tip: Use the gmtime()
or localtime()
function to get a tm
structure from a timestamp.
Note: Because the return value is a pointer, the value of the string may be changed by additional calls to asctime()
or ctime()
.
Syntax
asctime(const struct tm * time);
Parameter Values
Parameter | Description |
---|---|
time | Required. A tm structure representing the date and time to be represented. |
Technical Details
Returns: | A char type pointer to a C-style string containing a representation of the date and time. |
---|