C time (time.h) Library
C time Functions
The <time.h>
header provides functions for working with dates, times, and measuring durations in C.
A list of common time.h
functions can be found in the table below:
Function | Description |
---|---|
time() | Returns the current calendar time as a time_t value (seconds since Jan 1, 1970) |
localtime() | Converts a time_t value to local time and returns a pointer to a struct tm |
gmtime() | Converts a time_t value to UTC time (also as a struct tm ) |
ctime() | Converts a time_t value into a readable string (e.g. Thu Jun 26 10:30:00 2025 ) |
asctime() | Converts a struct tm to a string in a standard date/time format |
strftime() | Formats a struct tm into a custom date and time string |
difftime() | Calculates the difference (in seconds) between two time_t values |
mktime() | Converts a filled struct tm into a time_t value |
clock() | Returns the number of processor clock ticks used by the program (can be used to measure execution time) |
What is struct tm?
Several functions return or use a special structure called struct tm, which holds individual parts of the date and time like year, month, hour, and minute.
You can use this structure to format and access each time component separately.