C++_time

//将当前时间折算为秒级,再通过函数转换成时分秒
#include<iostream>  
#include<iomanip>
#include<ctime>  
using namespace std;
int main()
{
	time_t now_time;
	now_time = time(NULL);    // 获取当前时间
	cout << now_time << endl;

	struct tm time;
	localtime_s(&time, &now_time);    // 时间转换函数

	cout << setw(2) << setfill('0') << time.tm_hour << ":";    // 设置字符宽度与填充
	cout << setw(2) << setfill('0') << time.tm_min << ":";
	cout << setw(2) << setfill('0') << time.tm_sec;

	cin.get();
	return 0;
}

struct tm {
        int tm_sec;     /* 秒 - [0,59] */
        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;    /* 天 1 - [0,365] */
        int tm_isdst;   /* 夏令时 - [1,-1] */
        };

猜你喜欢

转载自blog.csdn.net/qq_37140815/article/details/81123081
今日推荐