Home > Net >  How can I parse a std::string (YYYY-MM-DD) into a COleDateTime object?
How can I parse a std::string (YYYY-MM-DD) into a COleDateTime object?

Time:07-05

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....

CodePudding user response:

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);

CodePudding user response:

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);
}
  • Related