背景需求:
需要将一个从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"));
CString strTime = oleDate.Format("%H:%M:%S");
int nDayOfWeek = oleDate.GetDayOfWeek();
CString str

在从Sql Server数据库通过Webservice获取到包含毫秒级数据的时间字符串后,如何正确转化为COleDateTime进行日期操作。错误示例中,由于字符串格式原因导致转换失败,正确方法能成功转化并显示详细日期和时间。
最低0.47元/天 解锁文章
884

被折叠的 条评论
为什么被折叠?



