C++ 获取系统时间,精确到毫秒并转成标准日志显示形式

C++ 获取系统时间,精确到毫秒并转成标准日志显示形式

c++通过windows API获取系统当前时间

调用Windows API

#include

通过系统API获取当前系统时间

SYSTEMTIME sysTime;
GetLocalTime(&sysTime);

转换成标准日志显示形式

void getSystemTimeString(string &TimeStr)
{
	SYSTEMTIME sysTime;
	GetLocalTime(&sysTime);
	char timeChar[128] = { 0 };
	sprintf(timeChar, "%04ld-%02ld-%02ld %02ld:%02ld:%02ld:%03ld", sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds);
	timeStr = timeChar;
	return;
}

Demo测试

void main()
{
	string showtime = "";
	getSystemTimeString(showtime);
	cout << showtime << endl;
	system("pause");
	return;
}

测试效果

时间打印

完整代码

#define _CRT_SECURE_NO_WARNING
#include
#include
#include
using namespace std;
void getSystemTimeString(string &TimeStr)
{
	SYSTEMTIME sysTime;
	GetLocalTime(&sysTime);
	char timeChar[128] = { 0 };
	sprintf(timeChar, "%04ld-%02ld-%02ld %02ld:%02ld:%02ld:%03ld", sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds);
	timeStr = timeChar;
	return;
}
void main()
{
	string showtime = "";
	getSystemTimeString(showtime);
	cout << showtime << endl;
	system("pause");
	return;
}

你可能感兴趣的:(C++)