2011-10-14 116 views
1

我正在以ColeDateTime格式從數據庫讀取日期時間。我想將其轉換爲CTime以獲取日期月份和時間。要將ColeDateTime轉換爲CTime

CString repDt; //**this will hold the datetime which i read from Database** 

COleDateTime dt; 
//**my datetime format is mm/dd/yyyy.** 

dt.ParseDateTime(repDt); **//this line of code gives exception** 

CTime t(dt.GetYear(), dt.GetMonth(), dt.GetDay(), dt.GetHour(), 
     dt.GetMinute(), dt.GetSecond()); 
m_time=t.GetTime(); 

請給我一些解決方案我怎麼能做到這一點?

在此先感謝。

+0

首先嚐試轉換repDT至今。 –

回答

4

CTime構造函數接受兩個SYSTEMTIMEDBTIMESTAMP結構,所以這兩個都可以工作:

SYSTEMTIME st; 
if (dt.GetAsSystemTime(st)) 
    t = CTime(st); 

DBTIMESTAMP dbts; 
if (dt.GetAsDBTIMESTAMP(dbts)) 
    t = CTime(dbts); 
1

請試試這個

COleDateTime dt=COleDateTime(2013, 12, 12,12,12,56); 
CTime ctime; 
SYSTEMTIME systime; 
dt.GetAsSystemTime(systime); 
ctime = CTime(systime);