首页 > 解决方案 > 我的 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.

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

标签: 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的文档。


推荐阅读