Common operation and comparison of VC + + CTime COleDateTime

Source: Internet
Author: User

using the COleDateTime class
1 to get the current time.
CTime time;
Time = Ctime::getcurrenttime ();
2) Gets the time element.
int year = time. GetYear ();
Int month = time. GetMonth ();
int day = time. Getday ();
int hour = time. Gethour ();
int minute = time. Getminute ();
int second = time. Getsecond ();
int DayOfWeek = time. GetDayOfWeek ();
3 Gets the time interval.
CTimeSpan TimeSpan (0,0,1,0); Days,hours,minutes,seconds
TimeSpan = Ctime::getcurrenttime ()-time;
4 Converts the time to a string.
CString sdate,stime,selapsed time;
Sdate = time. Format ("%m/%d/%y"); Ex:12/10/98
Stime = time. Format ("%h:%m:%s"); Ex:9:12:02
selapsed time = TimeSpan. Format ("%d:%h:%m:%s"); %d is total elapsed days
5 converts the string to a time.
CString Sdatetime;
int nyear, Nmonth, Ndate, Nhour, Nmin, NSec;
SSCANF (Sdatetime, "%d-%d-%d%d:%d:%d", &nyear, &nmonth, &ndate, &nhour, &nmin, &NSEC);
CTime stime (Nyear, Nmonth, Ndate, Nhour, Nmin, NSEC);
To find out more about the time format, see strftime in the MFC documentation

using the COleDateTime class
1 Get one day of the year.
COleDateTime datetime;
datetime = Coledatetime::getcurrenttime ();
int dayofyear = DateTime. Getdayofyear ();
2 reads the time from the text string.
COleDateTime datetime;
Datetime. ParseDateTime ("12:12:23 January 93");
3 Gets the time interval.
For example, calculate the date difference
COleDateTime begin_date (1970, 1, 1, 0, 0, 0);
COleDateTime end_date (1990, 1, 1, 0, 0, 0);
COleDateTimeSpan TimeSpan; Calculate Time Lag
TimeSpan = end_date-begin_date;
Long expi_date = Timespan.getdays ();

Description
CTime and COleDateTime have almost the same function. However, COleDateTime allows users to get one day of the year (a good way to create Julian dates), and to parse a time text string.
Compared with CTime, COleDateTime's advantage is that it supports DWORD variables. The number of digits used by COleDateTime is twice times that of a double floating-point, and since CTime simply calculates the number of seconds elapsed since January 1, 1970, it will reach 4294967295 by 2037 years, which can no longer be used. Instead, COleDateTime is a
A floating-point number that represents the number of days after December 30, 1900 (the small part of the day) and does not overflow within thousands of years.

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.