我的C ++程序如何访问计算机的日期和时间以仅打印和使用当前年份?

考虑用伪代码编写的程序:

//Tell program to access the system's date and time

//print just the 'current year',without having to print the entire time,date format.

//Be able to perform calculations with the printed number (for example: 2019),without having to manually enter it anywhere.

我的目标是弄清楚如何仅访问日期,月份或年份之类的单个信息,并使其成为程序内可用的操作数来执行操作。

leon_hit 回答:我的C ++程序如何访问计算机的日期和时间以仅打印和使用当前年份?

#include <iostream>
#include <ctime>

int main()
{
    time_t now = time(0);
    tm *ltm = localtime(&now);
    std::cout << "Year = " << 1900 + ltm->tm_year << std::endl;
}

请参阅ctime的文档。

本文链接:https://www.f2er.com/3169670.html

大家都在问