gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:16:02 25 4
gpt4 key购买 nike

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

//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.

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

最佳答案

#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 的文档.

关于c++ - 我的 C++ 程序如何访问计算机的日期和时间来打印和使用当前年份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58685767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com