结构类型的字段 tm 存储下面的值,其中每个为 int。
struct tm {
int tm_sec; // 秒,正常范围从 0 到 59,但允许至 61
int tm_min; // 分,范围从 0 到 59
int tm_hour; // 小时,范围从 0 到 23
int tm_mday; // 一月中的第几天,范围从 1 到 31
int tm_mon; // 月,范围从 0 到 11
int tm_year; // 自 1900 年起的年数
int tm_wday; // 一周中的第几天,范围从 0 到 6,从星期日算起
int tm_yday; // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起
int tm_isdst; // 夏令时
}
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <time.h>
#include <iomanip>
using std::setw;
using namespace std;
int main(void)
{
struct tm t; //tm结构指针
time_t now= time(0);; //声明time_t类型变量
time(&now); //获取系统日期和时间
localtime_s(&t, &now); //获取当地日期和时间
//格式化输出本地时间
cout << "年:" << setw(13) << t.tm_year +1900 << endl;
cout << "月:" << setw(13) << t.tm_mon + 1 << endl;
cout << "日:" << setw(13) << t.tm_mday << endl;
cout << "周:" << setw(13) << t.tm_wday << endl;
cout << "一年中:" << setw(13) << t.tm_yday << endl;
cout << "时:" << setw(13) << t.tm_hour << endl;
cout << "分:" << setw(13) << t.tm_min << endl;
cout << "秒:" << setw(13) << t.tm_sec << endl;
cout << "夏令时:" << setw(13) << t.tm_isdst << endl;
//将当前时间转化成字符串形式
char str[26];
ctime_s(str, sizeof str, &now);
cout << "本地日期和时间:" << str << endl;
return 0;
}