C++:您如何打印与当前时间相距特定时间的日期

C++: How do you a print a date a specific amount of time away from the current time?

本文关键字:时间 日期 定时间 何打印 打印 C++      更新时间:2023-10-16

我正在试验ctime但我一生都无法弄清楚如何打印距离当前日期 X 天的日期。这是我到目前为止的程序:

#include "header.h"
int main()
{
    time_t current;
    struct tm * timedata;
    time(&current);
    timedata = localtime(&current);
    cout << "The current date and time is: " << asctime(timedata) << endl << endl;
    return 0;
}

如果我是对的,time(&current)返回从 2000 年 1 月 1 日开始的秒数,但我得到的数字似乎太小了,无法做到这一点。所有的帮助都表示赞赏。

time(&current) 返回自 1970 年 1 月 1 日 UTC -0000 以来的秒数。

添加 n*86400,以获取当前时间/之前 n 天的时间。

当然,前提是你不会跨越夏令时/冬令时调整,如果你碰巧生活在世界上遭受这种奇怪概念困扰的地区......