Unreliable DateTime parsing by COleDateTime

506 views Asked by At

I am trying to parse the date using ParseDateTime method provided by COleDateTime class. But parsing of two different dates in the same program is returning inconsisent values for the month.

Code Snippet:

COleDateTime dtCreated;
dtCreated.ParseDateTime(safe_cast<CString>(strCreatedDate));

Inconsistent RESULTS:

if strCreatedDate = "10/7/2020" (in mm.dd.yyyy format) then dtCreated.GetMonth() = 7 (but it should be 10)

if strCreatedDate = "7/29/2020" (in mm.dd.yyyy format) then dtCreated.GetMonth() = 7 (in this case, it is correct)

UPDATE:

The value of date present in the strCreatedDate vairable could be "dd.mm.yyyy" OR "mm.dd.yyyy" format. But I do have the information about the data format available in a separate variable. Based on the format, I want COleDateTime to correctly parse the DateTime string. How can I do that?

1

There are 1 answers

7
Dialecticus On BEST ANSWER

Since String^ is your input you could use DateTime::ParseExact, and then convert DateTime to COleDateTime using DateTime.ToOADate:

COleDateTime dtCreated(DateTime::ParseExact(
    strCreatedDate, isDMY ? "d.M.yyyy" : "M.d.yyyy", nullptr).ToOADate());