1

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?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
user2756695
  • 676
  • 1
  • 7
  • 22
  • Try to use LCID parameter. – Dialecticus May 21 '21 at 09:41
  • @Dialecticus: I do have the information about the date format (dd.mm.yyyy OR mm.dd.yyyy) available in another variable. Can I somehow specify the correct format to use for the `ParseDateTime()`? The date is received from a server and it could be in (dd.mm.yyyy OR mm.dd.yyyy) format. – user2756695 May 21 '21 at 09:48
  • Okay if you have a bool, and you have only one exact format based on that bool then parse the date yourself with `sscanf`, or anything else you want. You get three values, and call `SetDateTime` with them. – Dialecticus May 21 '21 at 10:26
  • 1
    Date and time parsers have a history of producing [unexpected results](https://devblogs.microsoft.com/oldnewthing/20200929-05/?p=104313). It's not something I would rely on. I'd probably just write my own parser, really just a string split, followed by 3 applications of one of the [`stoi`](https://en.cppreference.com/w/cpp/string/basic_string/stol) family of functions. – IInspectable May 21 '21 at 10:38
  • BTW what kind of an object is `strCreatedDate`? This `safe_cast` looks out of place in this question. – Dialecticus May 21 '21 at 10:39
  • @Dialecticus: `strCreatedDate` is of type `String^` – user2756695 May 21 '21 at 10:43

1 Answers1

1

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());
Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • Why are you answering with a Managed C++/CLI answer, when the question is about Visual C++/MFC?! – sergiol May 21 '21 at 11:45
  • @sergiol because OP mentioned in the comments that `strCreateDate` is `String^`, which is logical when you notice `safe_cast` in the question. While the question itself can be viewed and answered strictly as MFC it can also be viewed and answered as C++/CLI + MFC. I chose the latter. – Dialecticus May 21 '21 at 11:50
  • `String^` could be C++/CLI or C++/CX though. Those are vastly different platforms, runtimes, and support libraries. – IInspectable May 21 '21 at 12:28
  • My source string (`String^ strCreateDate`) is indeed in C++/CLI. But with this implementation, it is throwing an exception, `{"String was not recognized as a valid DateTime."}` – user2756695 May 21 '21 at 12:55
  • Ok, the question comments were collapsed and I didn't see it. I could also deduce it from the question's `safe_cast`. – sergiol May 21 '21 at 13:02
  • @user2756695 since there are no leading zeros in the date it's better to use formats "d.M.yyyy" and "M.d.yyyy". I updated the answer. – Dialecticus May 21 '21 at 13:06
  • @Dialecticus: It worked. Actually, instead of using the boolean variable to create the format, I am directly passing the format to the `DateTime::ParseExact()`. – user2756695 May 21 '21 at 13:15