Number of days between two dates C++

Breakdown picture Breakdown · Jan 8, 2013 · Viewed 60.4k times · Source

I saw examples for C#, Java, but for C++ i cant find solution to calculate how many days between two dates.

For example between 2012-01-24 and 2013-01-08

Thanks!

Answer

sharafjaffri picture sharafjaffri · Jan 8, 2013

This is one way.

#include <iostream>
#include <ctime>

int main()
{
    struct std::tm a = {0,0,0,24,5,104}; /* June 24, 2004 */
    struct std::tm b = {0,0,0,5,6,104}; /* July 5, 2004 */
    std::time_t x = std::mktime(&a);
    std::time_t y = std::mktime(&b);
    if ( x != (std::time_t)(-1) && y != (std::time_t)(-1) )
    {
        double difference = std::difftime(y, x) / (60 * 60 * 24);
        std::cout << std::ctime(&x);
        std::cout << std::ctime(&y);
        std::cout << "difference = " << difference << " days" << std::endl;
    }
    return 0;
}

my output

Thu Jun 24 01:00:00 2004
Mon Jul 05 01:00:00 2004
difference = 11 days

Here is a ref to Original author post