写一段程序,在Windows下使用,输入一个日期,显示输入日期到当日时间长度
时间: 2024-05-10 10:15:41 浏览: 6
以下是使用C++编写的程序,在Windows下可以使用:
```cpp
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t now = time(NULL); // 获取当前时间
struct tm t = *localtime(&now); // 将当前时间转化为 struct tm 结构体
int year, month, day;
cout << "请输入日期(年 月 日):";
cin >> year >> month >> day;
// 设置用户输入的日期
t.tm_year = year - 1900;
t.tm_mon = month - 1;
t.tm_mday = day;
// 计算时间差
time_t diff = mktime(&t) - now;
int days = diff / 86400; // 一天的秒数为 86400
int hours = (diff % 86400) / 3600; // 一小时的秒数为 3600
int minutes = (diff % 3600) / 60; // 一分钟的秒数为 60
int seconds = diff % 60;
cout << "距离今天已经过去了 " << days << " 天 " << hours << " 小时 " << minutes << " 分钟 " << seconds << " 秒。" << endl;
return 0;
}
```
该程序首先获取当前时间,然后让用户输入一个日期,计算出用户输入的日期到当日时间长度,并输出。