背景需求:
需要将一个从SqlServer数据库中读取的时间字符串转化为COleDateTime,以便进行日期操作。
数据库读取为Webservice接口方式,传递到客户端即为字符串。
1、错误调用
CString strDateTime = _T("2011-09-28 14:51:18.640");
COleDateTime oleDate;
oleDate.ParseDateTime(strDateTime);
说明:strDateTime数据含有毫秒级数据,转化为日期格式时出错。
2、正确调用
CString strDateTime = _T("2011-09-28 14:51:18.640");
//除去豪秒int nIndex = strDateTime.ReverseFind('.');
strDateTime = strDateTime.Left(nIndex);
//转化为标准时间
COleDateTime oleDate;
oleDate.ParseDateTime(strDateTime);
CString strDate = oleDate.Format(_T("%Y-%m-%d"))