C difftime() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<time.h>

Definition

The C difftime() function subtracts one time_t calendar value from another and returns the interval in seconds as a double. The order is time2 - time1.

Use it instead of assuming that time_t is a plain integer with one unit per second. A positive result means the second argument in time order, time2, is later. Include <time.h>.

Syntax

Start with this concrete difftime() call:

C
difftime(finish, start);
CallWhat it does
difftime(finish, start);Returns the number of seconds from start to finish.

Declare the required objects with the time types shown below, include <time.h>, and check the documented result before using output data.

Library declaration
C
double difftime(time_t time2, time_t time1);

Parameters

ParameterDescription
time2Later (or comparison) calendar time.
time1Earlier (or baseline) calendar time.

Return Value

Return ValueDescription
Positive doubletime2 is later than time1.
ZeroThe calendar-time values represent the same instant.
Negative doubletime2 is earlier than time1.

C Examples

Example 1 — Find an Interval

This example demonstrates difftime() with a small, complete program and checks the relevant result before using it.

C
#include <stdio.h>
#include <time.h>

int main(void) {
    time_t start = (time_t)1000;
    time_t finish = (time_t)1090;
    printf("elapsed: %.0f seconds\n", difftime(finish, start));
    return 0;
}

How It Works

  1. Step 1. Create two representable sample calendar values.
  2. Step 2. Pass the later value first.
  3. Step 3. difftime() computes the interval portably.
  4. Step 4. Format the double with no decimal places for whole seconds.

Example 2 — Detect Earlier Time

This example demonstrates difftime() with a small, complete program and checks the relevant result before using it.

C
#include <stdio.h>
#include <time.h>

int main(void) {
    time_t first = (time_t)500;
    time_t second = (time_t)440;
    double gap = difftime(second, first);
    printf("difference: %.0f\n", gap);
    return 0;
}

How It Works

  1. Step 1. Use 500 as the baseline first instant.
  2. Step 2. Pass 440 as time2 even though it is earlier.
  3. Step 3. The subtraction order produces a negative interval.
  4. Step 4. The sign tells the relative ordering without comparing time_t representations directly.

Example 3 — Compare Local Dates

This example demonstrates difftime() with a small, complete program and checks the relevant result before using it.

C
#include <stdio.h>
#include <time.h>

int main(void) {
    struct tm a = {0}, b = {0};
    a.tm_year = b.tm_year = 124;
    a.tm_mon = b.tm_mon = 0;
    a.tm_mday = 1;
    b.tm_mday = 2;
    time_t first = mktime(&a);
    time_t second = mktime(&b);
    printf("hours: %.0f\n", difftime(second, first) / 3600.0);
    return 0;
}

How It Works

  1. Step 1. Build two local broken-down dates.
  2. Step 2. mktime() converts each structure to calendar time.
  3. Step 3. difftime() returns their interval in seconds.
  4. Step 4. Divide by 3600 to display hours; local clock changes can affect some dates.

Frequently Asked Questions

difftime() computes the difference between two calendar times in seconds.
Include <time.h> before calling difftime().
It returns time2 minus time1 as a double number of seconds. Positive means time2 is later; negative means earlier; zero means the same instant.
The result is calendar-time seconds, not processor time. For CPU work, use clock().
It is declared by the C time library; exact representations, locale, time-zone data, and resolution can still vary by implementation.

Next: gmtime()

Continue through the C time.h function tutorials.

gmtime() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful