C time time() Function
Example
Get the current time as a number of seconds:
time_t now = time(NULL);
printf("%lld", (long long)now);
Definition and Usage
The time() function returns the current calendar time as a time_t value. On most systems, it is the number of seconds since January 1, 1970, 00:00:00 UTC.
If the argument is not NULL, the time is also stored in the variable that it points to. So time(NULL) and time(&now) give the same result.
There is no format specifier for time_t, so the example converts the value to long long and prints it with %lld.
To show the time as a readable date, use ctime(), localtime() or strftime().
The time() function is defined in the <time.h> header file.
Syntax
time_t time(time_t *timer);
Parameter Values
| Parameter | Description |
|---|---|
| timer | Required. A pointer to a time_t variable to store the time in, or NULL. |
Technical Details
| Returns: | A time_t value with the current calendar time, or (time_t)(-1) if the time is not available. |
|---|
Related Pages
Read more about dates and times in our C Date and Time Tutorial.
Use time(NULL) as a seed for random numbers in our C Random Numbers Tutorial.