代码
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include<ctime>
using namespace std;
void use_localtime();
void use_localtime_s();
int main()
{
std::cout << "Hello World!\n";
use_localtime();
use_localtime_s();
}
void use_localtime() {
// 基于当前系统的当前日期/时间
time_t now = time(0);
tm* ltm = localtime(&now);
cout << "年: " << 1900 + ltm->tm_year << endl;
cout << "月: " << 1 + ltm->tm_mon << endl;
cout << "日: " << ltm->tm_mday << endl;
cout << "时间: " << ltm->tm_hour << ":";
cout << ltm->tm_min << ":";
cout << ltm->tm_sec << endl;
}
void use_localtime_s() {
// 基于当前系统的当前日期/时间
time_t now = time(0);
tm ltm;
localtime_s(<m, &now);
// 输出 tm 结构的各个组成部分
cout << "年: " << 1900 + ltm.tm_year << endl;
cout << "月: " << 1 + ltm.tm_mon << endl;
cout << "日: " << ltm.tm_mday << endl;
cout << "时间: " << ltm.tm_hour << ":";
cout << ltm.tm_min << ":";
cout << ltm.tm_sec << endl;
}
运行