2011-10-14 70 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);