visual如何在C++中比较两个日期
visual How to Compare two dates in C++?
你能告诉我是否有办法找到自过去一天以来花费的天数吗(请查看下面的代码)。如果我有一个包含2009年某一天的字符串,我如何将其与当前日期进行比较并显示它已经过去了多少天?
#include <time.h>
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int main ()
{
string olday = "05 14 2009";
const int MAXLEN = 80;
char newday[MAXLEN];
time_t t = time(0);
strftime(newday, MAXLEN, "%m %d %Y", localtime(&t));
cout <<"Current day is: "<<newday << 'n';
cout <<"Days spent since olday: "<<???? << 'n';
return 0;
}
Microsoft visual studio 2010 c++控制台
您可以使用difftime。
http://www.cplusplus.com/reference/clibrary/ctime/difftime/
由于它以秒为单位给出差异,因此很容易转换为天、月等。
首先需要将olday字符串转换为更可用的字符串。执行此操作的方法是创建一个struct tm
并填充值。然后将结构tm转换为具有mktime()
的time_t,并使用具有两个time_t
值的difftime()
。并将秒转换为天。
//create a local tm struct
struct tm old_day ;
//since it's a local, zero it out
memset(&old_day, 0, sizeof(struct tm)) ;
//fill in the fields
old_day.tm_year = 109 ; //years past 1900
old_day.tm_mon = 4 ;//0-indexed
//convert to a time_t
time_t t_old = mktime(&old_day) ;
相关文章:
- 如何找到两个日期之间的时间差异(以秒和纳秒为单位)?
- CPP 程序二查找两个给定日期之间的天数,conol CMD
- 解决方案在两个日期之间的天数内不起作用
- 使用 c++ 获取两个日期与时间字符串之间的差异(以秒为单位)
- C 的两个日期之间的差异
- 如何使用C Win32 API计算两个日期之间的天数
- 连接两个time_t值的日期和时间
- 如何比较C++中的两个日期
- 在C++中添加两个日期
- 两个日期之间的 C++ 日期差异
- 两个对象之间的日期比较
- visual如何在C++中比较两个日期
- 两个日期之间的总天数
- 两个日期之间的秒差
- 用c++计算两个日期的差异
- QDate -在两个日期之间搜索
- 如何比较程序中的两个日期
- C++-比较两个日期
- 比较来自字符串、char*或int的两个日期
- 如何使用boost::date_time获取两个日期之间的天数