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.