C time mktime() Function
Example
Find the day of the week for January 1, 2025:
struct tm date = {0};
date.tm_year = 2025 - 1900;
date.tm_mon = 0; // January
date.tm_mday = 1;
mktime(&date);
char days[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
printf("%s", days[date.tm_wday]);
Definition and Usage
The mktime() function converts a struct tm in local time into a time_t value.
It also fills in tm_wday (the day of the week) and tm_yday (the day of the year), and it fixes members that are out of range. For example, January 32 becomes February 1.
This makes mktime() useful for date calculations, like finding the day of the week, or adding days to a date (see More Examples below).
= {0} sets all members to 0 first. Then you only set the members you need. See all members on the C time Library page.
The mktime() function is defined in the <time.h> header file.
Syntax
time_t mktime(struct tm *timeptr);
Parameter Values
| Parameter | Description |
|---|---|
| timeptr | Required. A pointer to a struct tm with a local date and time. mktime() also changes this struct, as described above. |
Technical Details
| Returns: | A time_t value for the date and time, or (time_t)(-1) if it cannot be represented. |
|---|
More Examples
Example
Find the date 100 days after January 1, 2025:
struct tm date = {0};
date.tm_year = 2025 - 1900;
date.tm_mon = 0; // January
date.tm_mday = 1 + 100;
mktime(&date);
printf("Year: %d\n", date.tm_year + 1900);
printf("Month: %d\n", date.tm_mon + 1);
printf("Day: %d\n", date.tm_mday);
Related Pages
Read more about dates and times in our C Date and Time Tutorial.