CTime和CTimeSpan主要封装了ANSI time_t和关于time_t的Run-Time库的主要函数,CTime里面使用的成员变量是time_t类型,该类型是个long型,由于long 类型的原因,所以该类只能处理4294967296秒约68年的数据,所以用CTime只能处理1970年到2038年的日期。
CTime Class Members
Construction
CTime | Constructs CTime objects in various ways. |
GetCurrentTime | Creates a CTime object that represents the current time (static member function). |
Extraction
GetTime | Returns a time_t that corresponds to this CTime object. |
GetYear | Returns the year that this CTime object represents. |
GetMonth | Returns the month that this CTime object represents (1 through 12). |
GetDay | Returns the day that this CTime object represents (1 through 31). |
GetHour | Returns the hour that this CTime object represents (0 through 23). |
GetMinute | Returns the minute that this CTime object represents (0 through 59). |
GetSecond | Returns the second that this CTime object represents (0 through 59). |
GetDayOfWeek | Returns the day of the week (1 for Sunday, 2 for Monday, and so forth). 获取CTime对象代表的周日,1代表周日,2代表周一等 |
Conversion
GetGmtTm | Breaks down a CTime object into components — based on UTC. |
GetLocalTm | Breaks down a CTime object into components — based on the local time zone. |
GetAsSystemTime | Converts the time information stored in the CTime object to a Win32-compatibleSYSTEMTIME structure |
Format | Converts a CTime object into a formatted string — based on the local time zone. |
FormatGmt | Converts a CTime object into a formatted string — based on UTC. |
Operators
operator = | Assigns new time values. |
operator + – | Add and subtract CTimeSpan and CTime objects. |
operator +=, –= | Add and subtract a CTimeSpan object to and from this CTime object. |
operator ==, < , etc. | Compare two absolute times. |
Archive/Dump
operator << | Outputs a CTime object to CArchive or CDumpContext. |
operator >> | Inputs a CTime object from CArchive. |
CTimeSpan Class Members
Construction
CTimeSpan | Constructs CTimeSpan objects in various ways. |
Extraction
GetDays | Returns the number of complete days in this CTimeSpan. |
GetHours | Returns the number of hours in the current day (–23 through 23). |
GetTotalHours | Returns the total number of complete hours in this CTimeSpan. |
GetMinutes | Returns the number of minutes in the current hour (–59 through 59). |
GetTotalMinutes | Returns the total number of complete minutes in this CTimeSpan. |
GetSeconds | Returns the number of seconds in the current minute (–59 through 59). |
GetTotalSeconds | Returns the total number of complete seconds in this CTimeSpan. |
Conversion
Format | Converts a CTimeSpan into a formatted string. |
Operators
operator = | Assigns new time-span values. |
operator + – | Adds and subtracts CTimeSpan objects. |
operator += –= | Adds and subtracts a CTimeSpan object to and from this CTimeSpan. |
operator == < etc. | Compares two relative time values. |
Archive/Dump
operator << | Outputs a CTimeSpan object to CArchive or CDumpContext. |
operator >> | Inputs a CTimeSpan object from CArchive. |
CString s("2001-8-29 19:06:23");
int nYear, nMonth, nDate, nHour, nMin, nSec;
sscanf(s, "%d-%d-%d %d:%d:%d", &nYear, &nMonth, &nDate, &nHour, &nMin, &nSec);
CTime t(nYear, nMonth, nDate, nHour, nMin, nSec);
CString timestr = "2000年04月05日";
int a,b,c ;
sscanf(timestr.GetBuffer(timestr.GetLength()),"%d年%d月%d日",&a,&b,&c);
CTime time(a,b,c,0,0,0);
COleDateTime和COleDateTimeSpan两个类完全可以代替CTime和 CTimeSpan,COleDateTime和COleDateTimeSpan类所使用的成员变量是DATE类型,该类型是个double类型,而且使用的单位是日,所以可以处理从100年1月1日到9999年12月31日的日期时间,COleDateTime类的日期计算主要是操作公有成员变量 COleDateTime::m_dt,该变量是DATE即double类型,该变量是为零时是1899年12月30日0时0分0秒,大于零时的日期比 1899年12月30日0时0分0秒大,小于零比1899年12月30日0时0分0秒小。
COleDateTime Class Members
Construction
COleDateTime | Constructs a COleDateTime object. |
GetCurrentTime | Creates a COleDateTime object that represents the current time (static member function). |
Attributes
GetStatus | Gets the status (validity) of this COleDateTime object. |
SetStatus | Sets the status (validity) of this COleDateTime object. |
GetAsSystemTime | Converts the time in the COleDateTime object to be represented as aSYSTEMTIME data structure. |
GetYear | Returns the year this COleDateTime object represents. |
GetMonth | Returns the month this COleDateTime object represents (1 – 12). |
GetDay | Returns the day this COleDateTime object represents (1 – 31). |
GetHour | Returns the hour this COleDateTime object represents (0 – 23). |
GetMinute | Returns the minute this COleDateTime object represents (0 – 59). |
GetSecond | Returns the second this COleDateTime object represents (0 – 59). |
GetDayOfWeek | Returns the day of the week this COleDateTime object represents (Sunday = 1). |
GetDayOfYear | Returns the day of the year this COleDateTime object represents (Jan 1 = 1). 获得一年中的某一天 COleDateTime datetime; datetime=COleDateTime::GetCurrentTime(); int DayOfYear=datetime.GetDayOfYear(); |
Operations
SetDateTime | Sets the value of this COleDateTime object to the specified date/time value. |
SetDate | Sets the value of this COleDateTime object to the specified date-only value. |
SetTime | Sets the value of this COleDateTime object to the specified time-only value. |
Format | Generates a formatted string representation of a COleDateTime object. |
ParseDateTime | Reads a date/time value from a string and sets the value of COleDateTime. 从字符串中读取时间。 COleDateTime datetime; datetime.ParseDateTime("12:12:23 27 January 93"); |
Operators
operator DATE | Converts a ColeDateTime value into a DATE. |
operator DATE* | Converts a ColeDateTime value into a DATE*. |
operator = | Copies a COleDateTime value. |
operator +, - | Add and subtract ColeDateTime values. |
operator +=, -= | Add and subtract a ColeDateTime value from this COleDateTime object. |
operator ==, <, <=, etc. | Compare two ColeDateTime values. |
Data Members
m_dt | Contains the underlying DATE for this COleDateTime object. |
m_status | Contains the status of this COleDateTime object. |
Archive/Dump
operator << | Outputs a COleDateTime value to Carchive or CDumpContext. |
operator >> | Inputs a ColeDateTime object from CArchive. |
COleDateTimeSpan Class Members
Constructor
COleDateTimeSpan | Constructs a COleDateTimeSpan object. |
Attributes
GetStatus | Gets the status (validity) of this COleDateTimeSpan object. |
SetStatus | Sets the status (validity) of this COleDateTimeSpan object. |
GetDays | Returns the day portion of the span this COleDateTimeSpan object represents. |
GetHours | Returns the hour portion of the span this COleDateTimeSpan object represents. |
GetMinutes | Returns the minute portion of the span this COleDateTimeSpan object represents. |
GetSeconds | Returns the second portion of the span this COleDateTimeSpan object represents. |
GetTotalDays | Returns the number of days this COleDateTimeSpan object represents. |
GetTotalHours | Returns the number of hours this COleDateTimeSpan object represents. |
GetTotalMinutes | Returns the number of minutes this COleDateTimeSpan object represents. |
GetTotalSeconds | Returns the number of seconds this COleDateTimeSpan object represents. |
Operations
SetDateTimeSpan | Sets the value of this COleDateTimeSpan object. |
Format | Generates a formatted string representation of a COleDateTimeSpan object. |
Operators
operator double | Converts this COleDateTimeSpan value to a double. |
operator = | Copies a COleDateTimeSpan value. |
operator +, - | Add, subtract, and change sign for COleDateTimeSpan values. |
operator +=, -= | Add and subtract a COleDateTimeSpan value from this COleDateTimeSpan value. |
operator ==, <, <= | Compare two COleDateTimeSpan values. |
Data Members
m_span | Contains the underlying double for this COleDateTimeSpan object. |
m_status | Contains the status of this COleDateTimeSpan object. |
Dump/Archive
operator << | Outputs a COleDateTimeSpan value to CArchive or CDumpContext. |
operator >> | Inputs a COleDateTimeSpan object from CArchive. |
将CTime(或COleDateTime,方法一样的。)转换为CString的方法:
CTime tm = CTime::GetCurrentTime();
//取得CTime中的日期
CString cstrDate = tm.Format("%Y-%m-%d");
//取得CTime中的时间
CString cstrTime = tm.Format("%H:%M-%S");
CString szTime = tm.Format("'%Y-%m-%d %H:%M:%S'");
得到"2015-12-08 09:30:59"的格式,注意字母的大小写。
tm.Format(“%Y-%#m-%#d”);
得到的是2015-12-8格式的字符串时间,可以把月份与日的“0”去掉
COleDateTime转换为CTime:
SYSTEMTIME sys_time;
ole_time.GetAsSystemTime(sys_time);
c_time = CTime(sys_time);
CTime转换为COleDateTime:
SYSTEMTIME sys_time;
c_time.GetAsSystemTime(sys_time);
ole_time = COleDateTime(sys_time);