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!
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; }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With