C time difftime() Function
Example
Find the number of days between January 1 and March 1, 2025:
struct tm start = {0};
start.tm_year = 2025 - 1900;
start.tm_mon = 0; // January
start.tm_mday = 1;
struct tm end = start;
end.tm_mon = 2; // March
double seconds = difftime(mktime(&end), mktime(&start));
printf("%.0f days", seconds / (60 * 60 * 24));
Definition and Usage
The difftime() function returns the number of seconds between two time_t values, as a double.
It calculates end - start, so the result is negative if end is before start.
In the example, mktime() turns each date into a time_t value. Dividing the seconds by 60 * 60 * 24 (the number of seconds in a day) gives the number of days.
The difftime() function is defined in the <time.h> header file.
Syntax
double difftime(time_t end, time_t start);
Parameter Values
| Parameter | Description |
|---|---|
| end | Required. The later time. |
| start | Required. The earlier time. |
Technical Details
| Returns: | A double value with the number of seconds from start to end. |
|---|
Related Pages
Read more about dates and times in our C Date and Time Tutorial.