c++ - 我的 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.
我的目标是弄清楚如何仅访问日期、月份或年份等单个信息,并使其成为程序中可用的操作数以执行操作。
解决方案
#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的文档。
推荐阅读
- javascript - 想要在另一个组件中显示更新的状态值
- typescript - 数组的 TypeScript 类型推断
- java - 从 PDF 中过滤掉超过特定字体大小的所有文本
- ios - 条件中对“<”的模棱两可的引用 | 迅速
- ios - 使用 Swift 在输出流上发送字节
- python - Keras 神经网络模型在训练后不会像在 python 代码中那样在 android 代码中返回相同的预测
- python - Keras ValueError:检查目标时出错:预期dense_5具有形状(1,)但得到形状为(0,)的数组
- c# - In CMake, how to set a C# project's external references to specific versions?
- r - How do I remove duplicated columns from a data frame in R?
- angular - Angular - if statement to checking current route