C++ ctime time() Function
Example
Create a timestamp for the current moment in time:
time_t timestamp;
time(×tamp);
cout << ctime(×tamp);
Try it Yourself »
Definition and Usage
The time()
function writes a timestamp for the current date into the memory given by the argument and also returns the timestamp as a value.
The time()
function is defined in the <ctime>
header file.
The timestamp usually represents a number of seconds relative to the unix epoch (January 1, 1970) but it depends on how the library is implemented, so it is safer to only use it with functions designed to handle timestamps such as localtime()
and difftime()
.
Syntax
time(time_t * time);
The time_t
data type represents a number.
Parameter Values
Parameter | Description |
---|---|
time | Required. A pointer to the memory location where the timestamp will be written. |
Technical Details
Returns: | A time_t value representing the current date and time. |
---|
More Examples
Example
Another way to create a timestamp:
time_t timestamp = time(NULL);
cout << ctime(×tamp);
Try it Yourself »