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

时间:2014-04-23 11:44:22

标签: c++ visual-studio-2010 date time

需要转换字符串,该字符串以如下格式存储日期:“2014年4月23日12:39:17”到一个数字或对象;在特定于MS的环境中使用visual studio。

在C ++中是否有一个易于使用的功能可以实现这一目标?

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

感谢。

1 个答案:

答案 0 :(得分:1)

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

澄清:需要转换为某种日期对象的日期的字符串表示,以便我可以找到2个日期之间的差异。

这适用于MS VisualStudio2010&使用微软类。 (基本上,它不会在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);

希望这有助于某人!