在Visual Studio中将字符串转换为日期对象

Convert string to date object in Visual Studio

本文关键字:转换 日期 对象 字符串 Visual Studio      更新时间:2023-10-16

需要将一个字符串转换为数字或对象,该字符串以如下格式存储日期:"Apr 23 2014 12:39:17";在MS特定环境中与visual studio合作。

在C++中,是否有一个易于使用的函数可以实现这一点?

我这样做是为了比较字符串date和now()。

谢谢。

这是我为我的问题找到的解决方案。

澄清一下:日期的字符串表示需要转换成某种日期对象,这样我就可以找到两个日期之间的区别。

这适用于MSVisualStudio2010&使用microsoft类。(基本上,它在unix盒子上不起作用!)。

// Create 2 COleDateTime objects:
COleDateTime DateTime1;
COleDateTime DateTime2;
// 'Get' 2 string dates:
BSTR time1 = L"Apr 24 2014 09:20:20";
BSTR time2 = L"Apr 23 2014 12:39:17";
// Parse the string dates into the date objects (See! Its alot easier then I thought!)
DateTime1.ParseDateTime(time1);
DateTime2.ParseDateTime(time2);
// Calculate the time difference with a COleDateTimeSpan Object...
COleDateTimeSpan timeSpan = DateTime2 - DateTime1;
// Create integer with the difference in time in seconds...
CString str = timeSpan.Format(_T("%S"));
int differenceInSeconds = _tstoi(str);

希望这能帮助到别人!