我试图使用ParseDateTime类提供的COleDateTime方法来解析数据。但是,在同一个程序中解析两个不同的日期将返回这个月的不一致值。
代码片段:
COleDateTime dtCreated;
dtCreated.ParseDateTime(safe_cast<CString>(strCreatedDate));不一致结果:
如果strCreatedDate = "10/7/2020“( mm.dd.yyyy格式),那么dtCreated.GetMonth() =7(,但应该是10)
如果strCreatedDate = "7/29/2020“(以mm.dd.yyyy格式表示),那么dtCreated.GetMonth() =7(在本例中,是正确的)
更新:
strCreatedDate可变格式中的日期值可以是"dd.mm.yyyy“或"mm.dd.yyyy”格式。但我确实在一个单独的变量中获得了有关数据格式的信息。根据格式,我希望COleDateTime正确地解析DateTime字符串。我怎么能这么做?
发布于 2021-05-21 10:59:23
因为String^是您的输入,所以可以使用DateTime::ParseExact,然后使用DateTime.ToOADate将DateTime转换为COleDateTime
COleDateTime dtCreated(DateTime::ParseExact(
strCreatedDate, isDMY ? "d.M.yyyy" : "M.d.yyyy", nullptr).ToOADate());https://stackoverflow.com/questions/67634345
复制相似问题