How to get the difference between two dates in seconds with C

2 Answers

0 votes
#include <stdio.h>
#include <time.h>

double difference_between_two_dates_in_seconds(struct tm date1, struct tm date2) {
    // Convert tm structures to time_t
    time_t time1 = mktime(&date1);
    time_t time2 = mktime(&date2);

    // Calculate the difference in seconds
    return difftime(time2, time1);
}

int main() {
    // Define two tm structures for the dates
    struct tm date1 = {0};
    struct tm date2 = {0};

    // Set the first date (e.g., 2025-01-01 00:00:00)
    date1.tm_year = 2025 - 1900; // Year since 1900
    date1.tm_mon = 0;            // Month (0-11, where 0 = January)
    date1.tm_mday = 12;          // Day of the month (1-31)
    date1.tm_hour = 0;
    date1.tm_min = 0;
    date1.tm_sec = 0;

    // Set the second date (e.g., 2025-01-13 12:00:00)
    date2.tm_year = 2025 - 1900;
    date2.tm_mon = 0;
    date2.tm_mday = 13;
    date2.tm_hour = 0;
    date2.tm_min = 0;
    date2.tm_sec = 0;

    printf("Difference in seconds: %.0f\n", difference_between_two_dates_in_seconds(date1, date2));

    return 0;
}

    
     
/*
run:
      
Difference in seconds: 86400
    
*/

 



answered Jan 13, 2025 by avibootz
0 votes
#include <stdio.h>
#include <time.h>
 
/**
 * @brief Calculates the number of seconds between two struct tm date/time values.
 * 
 * @param start Pointer to the starting struct tm.
 * @param end Pointer to the ending struct tm.
 * @return double The difference in seconds (end - start), or -1.0 on error.
 */
double get_seconds_between_2_dates(struct tm *start, struct tm *end) {
    // Create local copies because mktime modifies the struct tm in place
    struct tm start_copy = *start;
    struct tm end_copy = *end;
 
    // Convert struct tm representations to time_t (calendar time in seconds since epoch)
    time_t start_time = mktime(&start_copy);
    time_t end_time = mktime(&end_copy);
 
    // Validate that conversion was successful
    if (start_time == (time_t)(-1) || end_time == (time_t)(-1)) {
        fprintf(stderr, "Error: Failed to normalize date structures.\n");
        return -1.0;
    }
 
    // Use difftime to reliably calculate the difference in seconds across different systems
    return difftime(end_time, start_time);
}
 
int main(void) {
    // Initialize start date: October 27, 2026, 12:00:00
    struct tm start_date = {0};
    start_date.tm_year = 2026 - 1900; // Years since 1900
    start_date.tm_mon = 10 - 1;       // Months since January (0 to 11)
    start_date.tm_mday = 27;          // Day of the month (1 to 31)
    start_date.tm_hour = 12;          // Hours (0 to 23)
    start_date.tm_min = 0;            // Minutes (0 to 59)
    start_date.tm_sec = 0;            // Seconds (0 to 60)
    start_date.tm_isdst = -1;         // Let mktime determine daylight saving time status
 
    // Initialize end date: October 27, 2026, 12:05:30
    struct tm end_date = {0};
    end_date.tm_year = 2026 - 1900;
    end_date.tm_mon = 10 - 1;
    end_date.tm_mday = 27;
    end_date.tm_hour = 12;
    end_date.tm_min = 5;
    end_date.tm_sec = 30;
    end_date.tm_isdst = -1;
 
    // Calculate seconds between the two dates
    double total_seconds = get_seconds_between_2_dates(&start_date, &end_date);
 
    if (total_seconds >= 0.0) {
        printf("Start Date: 2026-10-27 12:00:00\n");
        printf("End Date:   2026-10-27 12:05:30\n");
        printf("Total Seconds Between: %.0f\n", total_seconds);
    }
 
    return 0;
}
 
 
/*
run:
 
Start Date: 2026-10-27 12:00:00
End Date:   2026-10-27 12:05:30
Total Seconds Between: 330
 
*/
 

 



answered Oct 27 by avibootz
...