如何在C++中比较两个日期?

Jus*_*n k 1 c++ visual-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;
}
Run Code Online (Sandbox Code Playgroud)

Microsoft visual studio 2010 c ++控制台

Poc*_*chi 5

你可以使用difftime.

http://www.cplusplus.com/reference/clibrary/ctime/difftime/

由于它以秒为单位给出差异,因此很容易转换为天,月等.