user2756695
user2756695

Reputation: 696

Unreliable DateTime parsing by COleDateTime

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?

Upvotes: 1

Views: 589

Answers (1)

Dialecticus
Dialecticus

Reputation: 16779

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

Upvotes: 1

Related Questions