C++ 已知兩個時間(年月日)求日期差

轉載:https://blog.csdn.net/flyyufenfei/article/details/79796035ios

 

#include<iostream>    
#include <ctime>  

using namespace std;  

int main() {  
    struct tm t1 = { 0 };  
    struct tm t2 = { 0 };  
    double seconds;  

    t1.tm_year = 2019 - 1900; t1.tm_mon = 6; t1.tm_mday = 6;//如今時間2019,7,6  
    t2.tm_year = 2020 - 1900; t2.tm_mon = 5; t2.tm_mday = 7;//明年高考時間2020,6,7  

    seconds = difftime(mktime(&t2), mktime(&t1));//轉換結構體爲time_t,利用difftime,計算時間差  

    cout << seconds / 86400 << endl;//最後輸出時間,由於一天有86400秒(60*60*24)  
    system("pause");  
    return 0;  
}  
相關文章
相關標籤/搜索