c++ 获取当前时间
时间: 2023-11-25 07:50:42 浏览: 38
以下是两种C++获取当前时间的例子:
1.使用GetTickCount()函数获取当前时间,可精确到毫秒:
```cpp
#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 ";
return 0;
}
```
2.使用GetLocalTime()函数获取当前时间,可精确到秒:
```cpp
#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);
return 0;
}
```