1. Use CTime class (get system current time, accurate to second)
CString str;
Obtain the system time
CTime TM;
Tm=ctime::getcurrenttime ()//Get system date
Str=tm. Format ("Now time is%y year%m month%d");
MessageBox (STR,NULL,MB_OK);
A, the date of the month from the Ctimet
CTime t = ctime::getcurrenttime ();
int D=t.getday (); Get the number int y=t.getyear ();//
get
the year int m=t.getmonth ();//Get
the current month int h=t.gethour ()/////Get the current age
int mm= T.getminute (); Get the minute
int s=t.getsecond ();//Get the second
int w=t.getdayofweek ();//Get the day of the week, note 1 for Sunday, 7 for Saturday
b, to calculate the difference between two periods of time, you can use the CTimeSpan class, the specific use of the following methods:
CTime T1 (1999, 3, MB, 0);
CTime t = ctime::getcurrenttime ();
CTimeSpan span=t-t1; Calculates the interval int iday=span for the current system time and time T1
. GetDays (); Gets the total number of days int ihour=span at this time interval
. Gettotalhours (); Gets the total number of hours
int Imin=span. Gettotalminutes ()//Gets the total number of minutes
int Isec=span. Gettotalseconds ()//Get total number of seconds
C, get the current date and time and can be converted to CString
CTime tm=ctime::getcurrenttime ();
CString Str=tm. Format ("%y-%m-%d");//Show Month Day
2. Use the Getlocaltime:windows API function to obtain the local current system date and time (accurate to milliseconds)
This function stores the acquired system time information in the SYSTEMTIME structure.
typedef struct _SYSTEMTIME
{word
wyear;//year word
wmonth;//month
word wdayofweek;//week: 0 for Sunday, 1 for Monday, 2 for Tuesday ...
... Word wday;//day word
whour;//word
wminute;//Word
wsecond;//sec
word wmilliseconds;//milliseconds
} Systemtime,*psystemtime;
Cases:
SYSTEMTIME St;
CString Strdate,strtime;
Getlocaltime (&st);
Strdate.format ("%4d-%2d-%2d", st.wyear,st.wmonth,st.wday);
Strtime.format ("%2d:%2d:%2d", St.whour,st.wminute,st.wsecond);
AfxMessageBox (strdate);
AfxMessageBox (Strtime);
3. Use GetTickCount: The number of milliseconds from the operating system boot to the current (elapsed), and its return value is DWORD. (Accurate to milliseconds)
Get the program run time
Long T1=gettickcount (), or before the beginning of the program section to get the system running time (ms) sleep
();
Long T2=gettickcount ();()//The system run Time (MS) STR is obtained after the end of the program segment
. Format ("Time:%dms", t2-t1);//Before and after the difference is the program running time
AfxMessageBox (str);
Gets the system running time
long T=gettickcount ();
CString str,str1;
Str1. Format ("When system is running%d", t/3600000);
STR=STR1;
t%=3600000;
Str1. Format ("%d points", t/60000);
STR+=STR1;
t%=60000;
Str1. Format ("%d Seconds", t/1000);
STR+=STR1;
AfxMessageBox (str);
4. Use time_t time (time_t * timer): Use only C standard library (accurate to second)
Gets the number of seconds from the standard timer (usually midnight, January 1, 1970) to the current time
Calculation time difference: Double Difftime (time_t timer1, time_t Timer0)
struct TM *localtime (const time_t *timer); Obtain local time, the results obtained by the localtime are returned by the structure TM
The returned string can be determined in the following format:
%a the abbreviation of the week. Eg:tue
%A the full name of the week. Eg:tuesday
The abbreviation for the%b month name.
The full name of the%B month name.
%c Local End Date time is a better representation string.
%d numbers indicate the day of the month (range 00 to 31). Date
%H uses a 24-hour system number to indicate the number of hours (range 00 to 23).
%I A 12-hour number is used to indicate the number of hours (ranging from 01 to 12).
%j the number of days of the year (ranging from 001 to 366).
Number of%m months (ranging from 1 to 12).
%m minutes.
%p indicates the local end time with ' AM ' or ' PM '.
%s Number of seconds.
The%u number is expressed as the week ordinal of the year, and the first one week begins in Sunday.
The%w number is expressed as the week ordinal of the year, and the first one week begins in Monday.
%w the number of days of the week (0 for Sunday).
%x does not contain a date representation of time.
%x does not contain a time representation of dates. Eg:15:26:30
%y two digits indicates the year (range from 00 to 99).
%Y full year digits, that is, four digits. eg:2008
%Z (%Z) time zone or name abbreviation. EG: China Standard Time
%%% character.
The above mentioned is the entire content of this article, I hope you can enjoy.