帮我写一个2023倒计时的代码用C语言
时间: 2023-05-31 19:04:45 浏览: 95
#include <stdio.h>
#include <time.h>
int main()
{
time_t now, target;
struct tm target_time;
// 设置目标时间为2023年1月1日0时0分0秒
target_time.tm_year = 123; // 2023年
target_time.tm_mon = 0; // 1月
target_time.tm_mday = 1; // 1日
target_time.tm_hour = 0; // 0时
target_time.tm_min = 0; // 0分
target_time.tm_sec = 0; // 0秒
target_time.tm_isdst = -1; // 不考虑夏令时
target = mktime(&target_time);
while (1)
{
// 获取当前时间
now = time(NULL);
// 计算剩余时间
int diff = difftime(target, now);
int days = diff / (24 * 60 * 60); // 天数
int hours = (diff % (24 * 60 * 60)) / (60 * 60); // 小时数
int minutes = (diff % (60 * 60)) / 60; // 分钟数
int seconds = diff % 60; // 秒数
// 输出倒计时
printf("距离2023年1月1日0时0分0秒还有:%d天%d小时%d分%d秒\n", days, hours, minutes, seconds);
// 如果已经到达目标时间,则退出循环
if (diff <= 0)
{
printf("2023年已经到来!\n");
break;
}
// 等待1秒钟
sleep(1);
}
return 0;
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)