c/c++ 獲取當前時間 精確到毫秒或者秒


在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);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM