輸出當前日期到2021年1月1日的剩餘天數
阿新 • • 發佈:2020-12-21
#include
#include <time.h>
#include <stdio.h>
using namespace std;
time_t convert(int year, int mon, int day, int hour, int min, int sec, int week)
{
tm info;
info.tm_year = year-1900;
info.tm_mon = mon-1;;
info.tm_mday = day;
info.tm_hour = hour;
info.tm_min = min;
info.tm_sec = sec;
return mktime(&info); // mktime()函式可以將tm結構體轉化成秒值也就是time_t型別
}
int main()
{
int s_year, s_mon, s_day;
cout << “請輸入開始的日期(xxxx xx xx):” ;
cin >> s_year >> s_mon >> s_day ;
time_t start = convert(s_year, s_mon, s_day, 0, 0, 0, 0);
cout << “請輸入結束的日期(xxxx xx xx):” ;
cin >> e_year >> e_mon >> e_day;
time_t end = convert(e_year, e_mon, e_day, 0, 0, 0, 0);
int diff = (int) (end - start);
int days = diff/(606024);
cout << “中間相差了” << days << “天。” << endl;
system("pause");
return 0;
}