How to convert string to date in delphi

  • A+

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; 

How to convert string to date in delphi


Here is a full Example including uses needed My Favorit is tryStrToDateTime so you get the ability to check if the String is correct:

uses SysUtils;  ...  var  MyDatetimeString : String;  MyDatetime       : TDateTime; begin  MyDatetimeString:='2018-07-11T13:45:14.363';   if tryStrToDateTime(MyDatetimeString,MyDatetime) then  begin   showmessage('MyDateTime '+DateTimetoStr(MyDatetime));  end else begin   showmessage('String is not valid DateTime!');  end; end; 

10 Sec. internet research would have done the Job 100 times faster!


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: