【C++】获取当前时间的两种方式

下面是我查资料找到的我认为好用的两种方式,区别在于对localtime(&t)函数返回值的处理上。
我做成了getYear()和getMonth()函数,方便自己随时调用。

方法一:利用strftime()函数
strftime( ) 函数说明参考:https://blog.csdn.net/SanQi_Abao/article/details/94964665
可以获取的时间种类很多,这里只实现部分。

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

static string getYear() {
	time_t t = time(NULL);
	char ch[6] = { 0 };
	strftime(ch, sizeof(ch) - 1, "%Y", localtime(&t));
	return ch;
}
static string getMonth() {
	time_t t = time(NULL);
	char ch[4] = { 0 };
	strftime(ch, sizeof(ch) - 1, "%m", localtime(&t));
	return ch;
}
static string getDay() {
	time_t t = time(NULL);
	char ch[4] = { };
	strftime(ch, sizeof(ch) - 1, "%d", localtime(&t));
	return ch;
}
static string getHour() {
	time_t t = time(NULL);
	char ch[4] = { 0 };
	strftime(ch, sizeof(ch) - 1, "%H", localtime(&t));
	return ch;
}
static string getMinute() {
	time_t t = time(NULL);
	char ch[4] = { 0 };
	strftime(ch, sizeof(ch) - 1, "%M", localtime(&t));
	return ch;
}
static string getSecond() {
	time_t t = time(NULL);
	char ch[4] = { 0 };
	strftime(ch, sizeof(ch) - 1, "%S", localtime(&t));
	return ch;
}

int main() {
	cout << "year=" << getYear() << endl;
	cout << "month=" << getMonth() << endl;
	cout << "day=" << getDay() << endl;
	cout << "hour=" << getHour() << endl;
	cout << "minute=" << getMinute() << endl;
	cout << "second=" << getSecond() << endl;
	return 0;
}

在这里插入图片描述
方法二:利用结构体tm

//结构体tm内的成员
  int tm_sec;			/* Seconds.	[0-60] (1 leap second) */
  int tm_min;			/* Minutes.	[0-59] */
  int tm_hour;			/* Hours.	[0-23] */
  int tm_mday;			/* Day.		[1-31] */
  int tm_mon;			/* Month.	[0-11] */
  int tm_year;			/* Year	- 1900.  */
  int tm_wday;			/* Day of week.	[0-6] */
  int tm_yday;			/* Days in year.[0-365]	*/
  int tm_isdst;			/* DST.		[-1/0/1]*/
//看结构体成员就知道还能获取其他时间数据,我这里就不写全了
#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int getYear() {
	time_t t;
	time(&t);
	struct tm* tmp = localtime(&t);
	return tmp->tm_year + 1900;
}
int getMonth() {
	time_t t;
	time(&t);
	struct tm* tmp = localtime(&t);
	return tmp->tm_mon + 1;
}
int getDay() {
	time_t t;
	time(&t);
	struct tm* tmp = localtime(&t);
	return tmp->tm_mday;
}
int getHour() {
	time_t t;
	time(&t);
	struct tm* tmp = localtime(&t);
	return tmp->tm_hour;
}
int getMinute() {
	time_t t;
	time(&t);
	struct tm* tmp = localtime(&t);
	return tmp->tm_min;
}
int getSeond() {
	time_t t;
	time(&t);
	struct tm* tmp = localtime(&t);
	return tmp->tm_sec;
}


int main() {

	printf("year=%d, month=%d, day=%d, hour=%d, second=%d\n", getYear(),
			getMonth(), getDay(), getHour(), getSeond());
//year=2020, month=7, day=2, hour=9, second=3
}

========================================================
【文章为本人的学习笔记部分的分享,有任何错误地方欢迎评论指出,万分感谢!】

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值