gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:35:15 25 4
gpt4 key购买 nike

需要将存储日期格式为“Apr 23 2014 12:39:17”的字符串转换为数字或对象;在特定于 MS 的环境中使用 visual studio。

在 C++ 中是否有易于使用的函数可以实现此目的?

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

谢谢。

最佳答案

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

澄清一下:需要将日期的字符串表示形式转换为某种日期对象,这样我才能找到 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);

希望这对某人有帮助!

关于c++ - 在 Visual Studio 中将字符串转换为日期对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23243295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com