如何将STD :: String(Yyyy-MM-DD)放入coledateTime对象?

发布于 2025-02-12 18:00:24 字数 309 浏览 1 评论 0原文

我有一个coleDateTime对象,我想用格式yyyy-mm-dd来解析一个日期字符串。

例如,字符串变量是:

std::string strDate = "2022-07-04";

coledateTime允许我使用parsedateetime来解析字符串,但是我看不到它告诉它日期字符串的组件是什么格式是。在C#中,我可以使用dateTime.parse ...来做。

I have a COleDateTime object and I want to parse a date string in the format YYYY-MM-DD.

The string variable, for example, is:

std::string strDate = "2022-07-04";

COleDateTime allows me to use ParseDateTime to parse a string, but I see no way to tell it what format the components of the date string are. In C# I can do such with DateTime.Parse....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌辣 2025-02-19 18:00:25

为什么不只是简单的格式输入。

std::stringstream ss(strDate);
int year, month, day;
char dash;
ss >> year >> dash >> month >> dash >> day;
COleDateTime(year, month, day, 0, 0, 0);

Why not just a simple formatted input.

std::stringstream ss(strDate);
int year, month, day;
char dash;
ss >> year >> dash >> month >> dash >> day;
COleDateTime(year, month, day, 0, 0, 0);
放血 2025-02-19 18:00:25

根据@xmri的建议,我决定使用的评论:

CString strDutyMeetingDate = CString(tinyxml2::attribute_value(pDutyWeek, "Date").c_str());
int iDay{}, iMonth{}, iYear{};
if(_stscanf_s(strDutyMeetingDate, L"%d-%d-%d", &iYear, &iMonth, &iDay) == 3)
{
    const auto datDutyMeetingDate = COleDateTime(iYear, iMonth, iDay, 0, 0, 0);
}

Based on the suggestion by @xMRi in the comments I have decided to use:

CString strDutyMeetingDate = CString(tinyxml2::attribute_value(pDutyWeek, "Date").c_str());
int iDay{}, iMonth{}, iYear{};
if(_stscanf_s(strDutyMeetingDate, L"%d-%d-%d", &iYear, &iMonth, &iDay) == 3)
{
    const auto datDutyMeetingDate = COleDateTime(iYear, iMonth, iDay, 0, 0, 0);
}
水中月 2025-02-19 18:00:25

coledateTime :: parseDateTime使用默认参数lang_user_default,它可以称为

COleDateTime dt;
dt.ParseDateTime("2022-07-04");

dt.ParseDateTime("2022-07-04", VAR_DATEVALUEONLY, LANG_USER_DEFAULT);

“ 2022-07-07-04”使用长期格式要安全,因为很明显,这一年就在开始,预计每月将在中间。我相信任何LCID都应返回2022- 7月4日(我是60%肯定!)

如果日期字符串很短,它可能会与mm/dd/yy 混淆。格式,但这不是问题。

要手动制作LCID,请参见下面的英语示例,尽管在这种情况下不需要。

LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);

COleDateTime::ParseDateTime uses default parameter LANG_USER_DEFAULT, it can be called as

COleDateTime dt;
dt.ParseDateTime("2022-07-04");

Or

dt.ParseDateTime("2022-07-04", VAR_DATEVALUEONLY, LANG_USER_DEFAULT);

"2022-07-04" uses long date format so it should be safe, because it is clear that the year is at the start, and month is expected to be in the middle. I believe any LCID should return 2022-July-4th (I am 60% sure!)

If the date string was short, it could get confused with MM/DD/YY format, but that's not a problem here.

To make the lcid manually, see the English-US example below, although it should not be necessary in this case.

LCID lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文