C++ ctime ctime() Function
Example
Display the date and time of a timestamp as a string:
time_t timestamp;
time(×tamp);
cout << ctime(×tamp);
Try it Yourself »
Definition and Usage
The ctime() function returns a pointer to a C-style string representing the date and time of a time_t timestamp. 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 ctime() function is defined in the <ctime> header file.
Note: Use the time() or mktime() functions to create 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
ctime(time_t * timestamp);
The time_t data type represents a number.
Parameter Values
| Parameter | Description |
|---|---|
| time | Required. A time_t timestamp 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. |
|---|