ParseDateTime函数可以识别以下格式的时间字符串: The lpszDate parameter can take a variety of formats. For example, the following strings contain acceptable date/time formats:"25 January 1996" "8:30:00" "20:30:00" "January 25, 1996 8:30:00" "8:30:00 Jan. 25, 1996" "1/25/1996 8:30:00" 所以,"12:10:00"是可以直接识别的。
但我得到老是 8:00:00
COleVariant vtime(strCString);
vtime.ChangeType(VT_DATE);
COleDateTime time4=vtime;//COleDataTime--->CTime
SYSTEMTIME systime;
VariantTimeToSystemTime(time4, &systime);
CTime tm(systime);
COleDateTime dtTime;
dtTime.ParseDateTime(sTime);SYSTEMTIME st;
dtTime.GetAsSystemTime(st);
CTime tTime(st);就可以了。
The lpszDate parameter can take a variety of formats. For example, the following strings contain acceptable date/time formats:"25 January 1996"
"8:30:00"
"20:30:00"
"January 25, 1996 8:30:00"
"8:30:00 Jan. 25, 1996"
"1/25/1996 8:30:00" 所以,"12:10:00"是可以直接识别的。
MSDN说CString sTime = "12:10:00"; 这种可以
但像CString sTime = "12:10:00";
COleDateTime dtTime;
dtTime.ParseDateTime(sTime);SYSTEMTIME st;
dtTime.GetAsSystemTime(st);
CTime tTime(st); 这样得到的 老是那个默认值 8:00:00啊
修改一下:
CString sTime = "12:10:00";
COleDateTime dtTime;
dtTime.ParseDateTime(sTime);SYSTEMTIME st;
dtTime.GetAsSystemTime(st);
st.wYear = 1970;
CTime tTime(st);
{
if(date.IsEmpty())
return NULL;
if(date.GetLength()!=10)
return NULL;
date.TrimLeft();
date.TrimRight();
CString date_1=date.Left(4);
int date_years_i=atoi(date_1);//得到年份/
date=date.Right(5);
date_1=date.Left(2);
int date_month_i=atoi(date_1);//得到月份/
date=date.Right(2);
int date_day_i=atoi(date); //得到日期/
CTime time(date_years_i,date_month_i,date_day_i,0,0,0);
///}
// catch(_com_error &e)
/* {
CString err;
err.Format("日期时间转换错误\n",(char *)e.Description());
MessageBox(err,"错误提示",MB_OK|MB_ICONINFORMATION);
return false;
}*/
return time;
}
/*-----------------字符串转时间型*/
CTime CDXXX::StringToTime(CString date) /*字符串转编辑框时间型*/
{
if(date.IsEmpty())
return NULL;
if(date.GetLength()!=8)
return NULL;
date.TrimLeft();
date.TrimRight();
CString date_1=date.Left(2);
int time_hours_i=atoi(date_1);//得到小时/
date=date.Right(5);
date_1=date.Left(2);
int time_min_i=atoi(date_1);//得到分钟/
date=date.Right(2);
int time_sec_i=atoi(date);//得到秒/
CTime time(2005,5,17,time_hours_i,time_min_i,time_sec_i);
///}
// catch(_com_error &e)
/* {
CString err;
err.Format("日期时间转换错误\n",(char *)e.Description());
MessageBox(err,"错误提示",MB_OK|MB_ICONINFORMATION);
return false;
}*/
return time;
}