#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
*/