-1

I have an XML file that stored a date that is in a US date format (M/d/YYYY). In my application, I need to compare that date against the current date using DaysBetween. After reading the text value from the XML as a string, I try using StrUtils.StrToDateTime(string) on it.

This works when the PC is also operating with a US date format. But when I change this to a format that has a different arrangement of the date components like English (New Zealand) which is d/MM/yyyy, the `StrToDateTime' line errors out in my application saying something like:

'6/13/2015' is not a valid date and time.

With that date being what was read from the XML file.

Is there an intelligent way to use StrToDateTime and DaysBetween to compare a US formatted date (will always be this) with a different kind of date format (should work for any Windows locale)?

Gurfuffle
  • 784
  • 12
  • 32
ikathegreat
  • 2,311
  • 9
  • 49
  • 80

1 Answers1

3

Use the overloaded function StrToDateTime(const S: string; const AFormatSettings: TFormatSettings): TDateTime;, which takes a specified TFormatSetting as input.

Populate the TFormatSetting record according to your US locale setting.

Example:

myUSLocale := TFormatSettings.Create('en-US');
LU RD
  • 34,438
  • 5
  • 88
  • 296