13

When I convert string type to TDateTime I get an error. I'm using VarToDateTime function. My date as string is 2018-07-11T13:45:14.363.

var
  s: string;
  v: Variant;
  dt: TDateTime;
begin
  s := '2018-07-11T13:45:14.363';
  v := s;
  dt := VarToDateTime(v);
end;

enter image description here

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
Abdumalik Nabiev
  • 311
  • 1
  • 3
  • 14
  • 7
    It's difficult to answer this question, because you've provided no code. It might help if you read https://stackoverflow.com/a/3789628/62576 If it doesn't help, then [edit] your post to provide a [mcve] to show the specific problem you're having. – Ken White Jul 13 '18 at 05:06

3 Answers3

23

The success of a conversion from string to TDateTime using VarToDateTime depends on locale settings in the users system. The conversion fails if those settings do not match the string. This is the reason why the conversion fails on my system, as also on yours.


The primary option, if you are working with Delphi XE6 or later, is to use function ISO8601ToDate() as suggested by Marc Guillot in another answer

If you are workin with Delphi 2010 or later you can use the solution presented here.

Earlier versions than Delphi 2010 choke on the "T" in the input string, and may succeed if the "T" is removed or replaced with a space.


Use a conversion function which accepts a TFormatSetting that can be adjusted according to the string to convert. Such a function is the following overload of StrToDateTime() (See Embarcadero document)

function StrToDateTime(const S: string; const AFormatSettings: TFormatSettings): TDateTime;

Setting AFormatSettings to match the string to convert, ensures that the conversion succeeds:

procedure TForm3.Button1Click(Sender: TObject);
var
  fs: TFormatSettings;
  s: string;
  dt: TDateTime;
begin
  fs := TFormatSettings.Create;
  fs.DateSeparator := '-';
  fs.ShortDateFormat := 'yyyy-MM-dd';
  fs.TimeSeparator := ':';
  fs.ShortTimeFormat := 'hh:mm';
  fs.LongTimeFormat := 'hh:mm:ss';

  s := '2018-07-11T13:45:14.363';
  dt := StrToDateTime(s, fs);
end;
Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
7

These seems to be ISO8601 datetime strings : https://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations

So on Delphi XE 6 and later you can use the corresponding conversion function : ISO8601ToDate

http://docwiki.embarcadero.com/Libraries/XE8/en/System.DateUtils.ISO8601ToDate

But if you are using an older version of Delphi then you can use the XMLTimeToDateTime function on the XSBuiltIns unit to do that conversion (available since Delphi 6).

http://docwiki.embarcadero.com/Libraries/Tokyo/en/Soap.XSBuiltIns.XMLTimeToDateTime

Marc Guillot
  • 6,090
  • 1
  • 15
  • 42
  • 1
    Good finding +1, you may want to add a small detail: *... function on Delphi XE6 and later: ISO...* The function was added in XE6 and properly documented in XE8 docs. – Tom Brunberg Jul 13 '18 at 10:28
  • 1
    But Marc, it is available from **Delphi XE6** only – Tom Brunberg Jul 13 '18 at 12:40
  • @TomBrunberg Yes, unfortunately the OP hasn't said what Delphi version is he using. But you are right, on previous versions he needs to find another solution, like your customized converter. – Marc Guillot Jul 13 '18 at 13:05
  • 1
    @TomBrunberg in that case he has the XMLTimeToDateTime function on XSBuiltIns, available since Delphi 6. http://docwiki.embarcadero.com/Libraries/Tokyo/en/Soap.XSBuiltIns.XMLTimeToDateTime – Marc Guillot Jul 13 '18 at 13:28
-5

Try the function StrToDateTime which converts a string DateTime into a TDateTime value. Note that the datetime format passed should be the current system date/time format or else it will throw an exception. An example: StrToDateTime('2018-07-11 12:34:56');

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
Sydel
  • 34
  • 3
  • 3
    `StrToDateTime('2018-07-11 12:34:56')` results in an exception being raised here. Furthermore, the string in the question is of a different format. -1 – David Heffernan Jul 13 '18 at 10:44