1. Using the CTime class
CString str;
Get system time
CTime TM;
Tm=ctime:: Getcurrenttime_r ();
Str=tm. Format ("Now time is%y year%m month%d day%x");
MessageBox (STR,NULL,MB_OK);
2: Get system time date (using Getlocaltime)
SYSTEMTIME St;
CString Strdate,strtime;
Getlocaltime_r (&ST);
Strdate.format ("%4d-%2d-%2d", st.wyear,st.wmonth,st.wday);
Strtime.format ("%2d:%2d:%2d", St.whour,st.wminute,st.wsecond);
3. Using GetTickCount
Get program Run time
Long t1= gettickcount_r ();//Get System Run time (MS) before program section starts
Long t1= GetTickCount ();
Sleep (500);
Long t2= Gettickcount_r ();();//Get System Run time (MS) after the end of the program segment
Long t2= GetTickCount ();
Str. Format ("Time:%dms", t2-t1);//The difference between the front and back is the program run time
AfxMessageBox (str);
4. Get the system run time
Long t= gettickcount_r ();
CString str,str1;
Str1. Format ("System is running%d", t/3600000);
STR=STR1;
t%=3600000;
Str1. Format ("%d", t/60000);
STR+=STR1;
t%=60000;
Str1. Format ("%d Seconds", t/1000);
STR+=STR1;
AfxMessageBox (str);
5. Calculate the number of seconds from January 1, 1970 0:0 0 seconds to that point in time
#include <iostream>
#include <ctime>
using namespace Std;
int main () {
time_t Now_time;
Now_time = time (NULL);
cout<<now_time;
return 0;
}
6. Use the system function to change the computer's time setting
#include <stdlib.h>
#include <iostream>
using namespace Std;
void Main () {
System ("Time");
}
VC Get current Time