VC ++ obtains the system time and program running time (precise to seconds and milliseconds ).

Source: Internet
Author: User
Tags month name

VC ++ obtains the system time and program running time (precise to seconds and milliseconds ).

1. Use the CTime class (get the current system time, accurate to seconds)
CString str; // obtain the system time CTime tm; tm = CTime: GetCurrentTime (); // obtain the system date str = tm. format ("the current time is % Y % m month % d % X"); MessageBox (str, NULL, MB_ OK );
A. Extract year, month, day, hour, minute, and second from 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 int h = t of the current month. getHour (); // get the current time int mm = t. getMinute (); // get the minute int s = t. getSecond (); // get the second int w = t. getDayOfWeek (); // obtain the day of the week. Note that 1 is Sunday and 7 is Saturday.
B. The CTimeSpan class can be used to calculate the difference between the two periods. The usage is as follows:
CTime t1 (1999, 3, 19, 22, 15, 0); CTime t = CTime: GetCurrentTime (); CTimeSpan span = t-t1; // calculate the interval between the current system time and time t1. int iDay = span. getDays (); // obtain the total number of days of the interval. int iHour = span. getTotalHours (); // get the total number of hours int iMin = span. getTotalMinutes (); // obtain the total number of minutes int iSec = span. getTotalSeconds (); // get the total number of seconds
C. Obtain the current date and time and convert it to CString.
CTime tm = CTime: GetCurrentTime (); CString str = tm. Format ("% Y-% m-% d"); // display year, month, and day
2. Use the GetLocalTime: Windows API function to obtain the current local system date and time (accurate to milliseconds). This function stores the obtained system time information in the SYSTEMTIME struct.
Typedef struct _ SYSTEMTIME {WORD wYear; // year WORD wMonth; // month WORD wDayOfWeek; // week: 0 is Sunday, 1 is Monday, 2 is Tuesday ...... WORD wDay; // day WORD wHour; // hour WORD wMinute; // minute WORD wSecond; // Second WORD wMilliseconds; // millisecond} SYSTEMTIME, * PSYSTEMTIME;
Example:
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 that have elapsed since the operating system was started (elapsed). The returned value is DWORD. (Precise to milliseconds)
// Get the program running time long t1 = GetTickCount (); // get the system running time before the program segment starts (MS) Sleep (500); long t2 = GetTickCount ();(); // get the system Runtime (MS) str after the program segment ends. format ("time: % dms", t2-t1); // The difference between front and back is the running time AfxMessageBox (str); // get the system running time long t = GetTickCount (); CString str, str1; str1.Format ("when the 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 );
4. use time_t time (time_t * timer): only use the C Standard Library (accurate to seconds) to get the time difference in seconds from the standard time point (usually midnight on January 1, January 1, 1970) to the current time: double difftime (time_t timer1, time_t timer0) struct tm * localtime (const time_t * timer); get local time, the result obtained by localtime is returned by the structure tm. The returned string can be in the following format: % a abbreviation of the day of the week. Eg: Full name of Tue % A day of the week. Eg: Abbreviation of Tuesday % B month name. % B full name of the month name. % C the local date time is better than the string. % D indicates the day of the month (range: 00 to 31) with a number ). The date % H represents the hour in the 24-hour format (range: 00 to 23 ). % I represents the hour in 12-hour format (range: 01 to 12 ). % J indicates the day of the year (range: 001 to 366 ). The number of % m months (ranging from 1 to 12 ). % M minutes. % P uses ''am'' or ''pm ''to indicate the local time. % S seconds. % U indicates the week of the current year. The first week starts from the first Sunday. % W indicates the week of the current year. The first week starts from the first Monday. % W indicates the day of the week by number (0 indicates Sunday ). % X date representation without time. % X does not include the time representation of the date. Eg: 15:26:30% y two digits indicate the year (range: 00 to 99 ). % Y indicates the complete year number, that is, the four-digit number. Eg: 2008% Z (% z) Time Zone or abbreviation. Eg: Chinese Standard Time % characters. Including: year, month, day, week, hour, minute, second, millisecond. [Drinking wine] http://blog.const.net.cn/a/16370.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.