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


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

 

#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;  
}  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM