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

3 Answers

0 votes
#include <iostream>
#include <ctime>

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

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

int main() {
    // Define two tm structures for the dates
    std::tm date1 = {0};
    std::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;

    std::cout << "Difference in seconds: " << difference_between_two_dates_in_seconds(date1, date2);
}

 
     
/*
run:
      
Difference in seconds: 86400
    
*/

 



answered Jan 13, 2025 by avibootz
0 votes
#include <iostream>
#include <string>
#include <chrono>
#include <iomanip> // for std::get_time

int main() {
    std::string date1_str = "2025-01-13 00:00:00";
    std::string date2_str = "2025-01-12 00:00:00";

    std::tm t1 = {};
    std::tm t2 = {};

    std::istringstream ss1(date1_str);
    std::istringstream ss2(date2_str);

    ss1 >> std::get_time(&t1, "%Y-%m-%d %H:%M:%S");
    ss2 >> std::get_time(&t2, "%Y-%m-%d %H:%M:%S");

    if (ss1.fail() || ss2.fail()) {
        std::cerr << "Failed to parse time string\n";
        return 1;
    }

    std::time_t time1 = mktime(&t1);
    std::time_t time2 = mktime(&t2);

    if (time1 == -1 || time2 == -1) {
        std::cerr << "Failed to convert to time_t\n";
        return 1;
    }

    auto diff_seconds = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::from_time_t(time1) -
                                                                         std::chrono::system_clock::from_time_t(time2)).count();

    std::cout << "Difference using chrono (seconds): " << diff_seconds << std::endl;
}

 
     
/*
run:

Difference using chrono (seconds): 86400
    
*/

 



answered Jan 13, 2025 by avibootz
0 votes
#include <chrono>
#include <iostream>

using namespace std;

// Returns the number of seconds between two dates.
// Positive if endDate is after startDate, negative otherwise.
long long secondsBetweenDates(
    const chrono::year_month_day& startDate,
    const chrono::year_month_day& endDate)
{
    // Convert calendar dates to time points.
    chrono::sys_days start{startDate};
    chrono::sys_days end{endDate};

    // Compute the duration and convert to seconds.
    auto duration = end - start;
    auto seconds = chrono::duration_cast<chrono::seconds>(duration);

    return seconds.count();
}

int main()
{
    // Create two dates: YYYY/MM/DD
    chrono::year_month_day startDate{
        chrono::year{2024},
        chrono::month{1},
        chrono::day{1}
    };

    chrono::year_month_day endDate{
        chrono::year{2024},
        chrono::month{12},
        chrono::day{31}
    };

    long long seconds = secondsBetweenDates(startDate, endDate);

    cout << "Seconds between dates: "
         << seconds << '\n';
}


/*
run:

31536000

*/

 



answered Oct 27 by avibootz
...