求教:在C++中的日期问题
怎么计算两天之间差的天数??如1988.3.11和2005.5.12
谢谢
#include <iostream> #include <time.h> using namespace std; int main() { struct tm t1, t2; double day_number = 0; time_t t_of_t1, t_of_t2; t1.tm_year = 1988 - 1900; t2.tm_year = 2005 - 1900; t1.tm_mon = 2; // 3 月 t2.tm_mon = 4; // 5 月 t1.tm_mday = 11; t2.tm_mday = 12; t1.tm_hour = t2.tm_hour = 0; t1.tm_min = t2.tm_min = 0; t1.tm_sec = t2.tm_sec = 1; t1.tm_isdst = t2.tm_isdst = 0; t_of_t1 = mktime(&t1); t_of_t2 = mktime(&t2); day_number = difftime(t_of_t2, t_of_t1); cout<<"Days from 03-11-1988 To 05-12-2005 is:"<<(int)(day_number/(24*3600))<<endl; return 0;
} output: Days from 03-11-1988 To 05-12-2005 is:6271 Press any key to continue...