VC/mfc time

Source: Internet
Author: User
Tags filetime

1. Get the current time

Note that getlocaltime gets the local time zone, while getsystemtime gets the UTC time.

Coledatetime ODT = coledatetime: getcurrenttime ();
String STR = ODT. Format ("% Y-% m-% d % H: % m: % s ");
For more information about how to use coledatetime: format, see the description of strftime and wcsftime In the CTR library.
Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/vclib/html/_ crt_strftime.2c _. wcsftime. asp


2. add or subtract a period of time based on the specified time to obtain the new date and time // MFC:
Coledatetime ODT = coledatetime: getcurrenttime (); // get the current time
Coledatetimespan odts;
Odts. setdatetimespan (0, 15, 0, 0); // time difference of 15 hours
ODT + = odts;
Cstring STR = ODT. Format ("% Y-% m-% d % H: % m: % s ");

// SDK:
// Filetime is measured in 100 nanoseconds.
// The following macro defines the number of units in an hour
# Define nano100_sec_in_hour (ulonglong) 60*60*10000000

Systemtime st;
Getlocaltime (& St );
Filetime Ft = {0 };
Systemtimetofiletime (& St, & ft );
Ularge_integer UI;
Ui. lowpart = ft. dwlowdatetime;
Ui. highpart = ft. dwhighdatetime;
Ui. quadpart + = 16 * nano100_sec_in_hour; // The current time plus the time after 16 hours
Ft. dwlowdatetime = UI. lowpart;
Ft. dwhighdatetime = UI. highpart;
Filetimetosystemtime (& ft, & St );
// The variable in the St struct is the final result.

3. Based on the given date, obtain the number of weeks of the date, and then obtain the start and end dates of the week.

At that time, this function was completed by helping a mm .. Haha

Coledatetime T1, tweekbegin, tweekend;
T1.setdate (, 8 );
// It should be noted that Westerners have different habits than us. They regard Sunday as the first day of every week. The following function getdayofweek is the same, so I made the following conversions.
Int nxingqi = t1.getdayofweek ();
Coledatetimespan tsweekbegin, tsweekend;
If (1 = nxingqi)
{
Tsweekbegin. setdatetimespan (6, 0, 0, 0 );
Tsweekend. setdatetimespan (0, 0, 0, 0 );
}
Else
{
Tsweekbegin. setdatetimespan (nXingQi-2, 0, 0, 0 );
Tsweekend. setdatetimespan (8-nxingqi, 0, 0, 0 );
}
Tweekbegin = T1-tsweekbegin;
Tweekend = t1 + tsweekend;
Cstring str1 = tweekbegin. Format ("% Y-% m-% d ");
Cstring str2 = tweekend. Format ("% Y-% m-% d ");

 

4. cstring-> coledatetime cstring STR = "11:22:33 ";
Colevariant varianttime;
Varianttime = STR;
Varianttime. changetype (vt_date );
Coledatetime datatime = varianttime;
Int nyear = datatime. getyear ();
Int nmonth = datatime. getmonth ();
Int nday = datatime. getday ();
Int nhour = datatime. gethour ();
Int Nmin = datatime. getminute ();
Int nsec = datatime. getsecond ();

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.