两个日期之间的天数C ++ [英] Number of days between two dates C++
本文介绍了两个日期之间的天数C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如2012-01-24和2013-01-08
谢谢!
解决方案
一种方式。
#include< iostream>
#include< ctime>
int main()
{
struct std :: tm a = {0,0,0,24,5,104}; / * 2004年6月24日* /
struct std :: tm b = {0,0,0,5,6,104}; / * 2004年7月5日* /
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 =<<差异< 天< std :: endl;
}
return 0;
}
我的输出
星期四24 01:00:00 2004
Mon Jul 05 01:00:00 2004
差= 11天
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;
}
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
这篇关于两个日期之间的天数C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文