CString转化为COleDateTime

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

背景需求:

需要将一个从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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值