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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

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

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

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

2 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

Related questions

1 answer 79 views
1 answer 75 views
1 answer 95 views
1 answer 80 views
1 answer 84 views
1 answer 81 views
...