Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,700 questions

57,013 answers

573 users

How to get the number of seconds between two dates in C

1 Answer

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 3 hours ago by avibootz
...