1 获取当前时间
CTime time = CTime::GetCurrentTime();
其中GetYear( ),GetMonth( ), GetDay( ), GetHour( ), GetMinute( ), GetSecond( ), GetDayOfWeek( ) 返回整型(int)对应项目
例:
int NowMonth = time1.GetMonth();
2 两个时间之间的比较
BOOL operator ==( CTime time ) const;
BOOL operator !=( CTime time ) const;
BOOL operator <( CTime time ) const;
BOOL operator >( CTime time ) const;
BOOL operator <=( CTime time ) const;
BOOL operator >=( CTime time ) const;
例:
CTime t1 = CTime::GetCurrentTime();
CTime t2 = t1 + CTimeSpan( 1, 0, 0, 0 ); // 1 Day later
ASSERT( t1 != t2 );
ASSERT( t1 < t2 );
ASSERT( t1 <= t2 );
3 时间差
CTime operator +( CTimeSpan timeSpan ) const;
CTime operator - ( CTimeSpan timeSpan ) const;
CTimeSpan operator - ( CTime time ) const;
例:
CTime t1( 1999, 3, 19, 22, 15, 0 ); // 10:15PM March 19, 1999
CTime t2( 1999, 3, 20, 22, 15, 0 ); // 10:15PM March 20, 1999
CTimeSpan ts = t2 - t1; // Subtract 2 Ctimes
ASSERT( ts.GetTotalSeconds() == 86400L );
ASSERT( ( t1 + ts ) == t2 ); // Add a CTimeSpan to a CTime.
ASSERT( ( t2 - ts ) == t1 ); // Subtract a CTimeSpan from a CTime.
4 时间格式化
将当前时间格式化 CString date = time.Format("%Y-%m-%d %H:%M:%S %W-%A");
结果为:2006-10-13 17:23:47 41-Friday
阅读(2580) | 评论(0) | 转发(0) |