VC 中CTime 常用转换

CTime 对象转换为 DWORD 类型

CTime time=CTime::GetCurrentTime(); //获取当前时间
DWORD timedw = time.GetTime(); //CTime->DWORD


DWORD 类型变量转换为 CTime 对象

CTime time1(timedw); //DWORD->CTime
CString strTime;
strTime.Format( "%d-%d-%d %d:%d:%d",
time1.GetYear(),time1.GetMonth(),time1.GetDay(),time1.GetHour(),time.GetMinute(),time.GetSecond()); //CTime的格式化显示


CString 转 COleDateTime

//方法1:COleDateTime::ParseDateTime()
CString strDate = _T("1987-06-21");
COleDateTime oleDate;
oleDate.ParseDateTime(strDate, VAR_DATEVALUEONLY);
//方法2:COleVariant
CString strDate = _T("1987-06-21");
COleVariant varTime(strDate);
varTime.ChangeType(VT_DATE);
COleDateTime oleDate(varTime);
//COleDateTime oleDate;
//oleDate=varTime;


COleDateTime 转 CTime

//方法1:用COleDateTime::GetAsSystemTime(SYSTEMTIME& sysTime)
COleDateTime oleDate(1987, 6, 21, 0, 0, 0);
SYSTEMTIME sysTime;
oleDate.GetAsSystemTime(sysTime);
CTime tm(sysTime);
//方法2:用VariantTimeToSystemTime函数
COleDateTime--->SYSTEMTIME--->CTime
COleDateTime oleDate(1987, 6, 21, 0, 0, 0);
SYSTEMTIME sysTime;
VariantTimeToSystemTime(oleDate, &sysTime);
CTime tm(sysTime);


CTime 转 COleDateTime

//方法1:用CTime::GetTime()得到time_t
CTime time(1987, 6, 21, 0, 0, 0);
time_t tm = time.GetTime();
COleDateTime oleTime(tm);
//方法2:用COleDateTime::GetAsSystemTime(SYSTEMTIME& sysTime)
CTime time(1987, 6, 21, 0, 0, 0);
SYSTEMTIME sysTime;
time.GetAsSystemTime(sysTime);
COleDateTime oleTime(sysTime);
//COleDateTime oleTime;
//oleTime=sysTime;