在window环境下:
1.精确到毫秒
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int main(int argc, _TCHAR* argv[])
{
DWORD time_start, time_end;
/* 获取开始时间 */
time_start = GetTickCount(); //从操作系统启动经过的毫秒数
Sleep(3000);
time_end = GetTickCount();
cout << "Time = " << (time_end - time_start) << "ms\n ";
system("pause");
return 0;
}
2.精确到秒
#include <windows.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
SYSTEMTIME st = { 0 };
GetLocalTime(&st); //获取当前时间 可精确到ms
printf("%d-%02d-%02d %02d:%02d:%02d\n",
st.wYear,
st.wMonth,
st.wDay,
st.wHour,
st.wMinute,
st.wSecond);
}