C++的时间函数

C++中并没有针对时间特意提供特定的时间类型,而是直接继承了C语言的结构以及函数,因此在C++中使用时间函数需要引用<ctime>头文件。

//时间的结构体
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; // 夏令时
};

 编译环境VS2019 

#include <iostream>
#include <ctime>
using namespace std;
#define TIMESIZE 26
int main() {
	
	time_t now = time(0);
	char dt[TIMESIZE];
	errno_t err;
	err = ctime_s(dt, TIMESIZE, &now);
	cout << "local time: " << dt << endl;
	cout << "timestamp: " << now << endl;
	struct tm ltm;
	localtime_s(&ltm , &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 << ":"<< ltm.tm_sec << endl;
	system("pause");
	return 0;
}

效果

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃瓜太狼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值