3

I have a string containing a date, and another string containing the date format of the first string. Is there a function that I can call to convert that date into something like a SYSTEMTIME structure? Basically, I'd like the opposite of GetDateFormat().

Andy
  • 30,088
  • 6
  • 78
  • 89
  • If you don't want to use MFC you can use VarDateFromStr directly. [VarDateFromStr ](https://learn.microsoft.com/en-us/windows/desktop/api/oleauto/nf-oleauto-vardatefromstr) – BOFHRAF Jun 06 '19 at 15:00

4 Answers4

3

It doesn't do quite what you've described, but I'd start by using COleDateTime::ParseDateTime. It works with localized date-time formats.

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
  • I saw that function, and given that it doesn't take a format, it seems like it could get confused in some cases (month/day/year vs day/month/year). – Andy Oct 25 '08 at 18:38
  • It does take a locale though, so that should help. I can't remember whether it does or not: give it a try. – Anthony Williams Oct 25 '08 at 21:06
2

No, but you can write a function that will convert the date format string into a sscanf format string and series of variables. Then copy the data from the variables to the SYSTEMTIME structure and you're all set.

It's not trivial, but it is probably the most flexible arrangement if you must support many different date formats.

Ajay
  • 18,086
  • 12
  • 59
  • 105
Adam Davis
  • 91,931
  • 60
  • 264
  • 330
0

In the source file Here.

I have a function that reads dates in a bunch of different formats, the function is called

bool kGUIDate::Setz(const char *datestring)

It's a class that reads in the day / month / year and also handles hours / minutes / seconds.

The code is LGPL so feel free to copy it.

/* formats excepted are: */
/* yyyy-mm-dd */
/* Wdy, DD-Mon-YY HH:MM:SS GMT */
/* Wdy, D Mon YY HH:MM:SS GMT */
KPexEA
  • 16,560
  • 16
  • 61
  • 78
0

If you have a POSIX system, you can use the strptime() function. Unfortunately, MSVC 2005 does not seem to have this function in its C runtime; MinGW for Windows does.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589