Delphi : Convert a String to TDateTime

In Delphi concerting string to TDateTime is always an issue for me: Here is the code to help someone.

[sourcecode language=”pascal” wraplines=”false” collapse=”false”]

var
myformat : TFormatSettings;
theDate : TDateTime;
begin

GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, myformat );
myformat.DateSeparator := ‘/’;
myformat.TimeSeparator := ‘:’;
myformat.ShortDateFormat := ‘dd/mm/YYYY hh:nn’;
theDate := StrToDateTime( ’01/01/2017′,myformat);
end;
[/sourcecode]

Hope this helps someone.

The inverse is easier:
[sourcecode language=”pascal” wraplines=”false” collapse=”false”]

MyStr := FormatDateTime(‘dd/mm/yyyy HH:nn:ss’,theDate));
[/sourcecode]

If you have the Project Jedi Components JVCL Delphi Jedi you can use the
[sourcecode language=”pascal” wraplines=”false” collapse=”false”] JvJCLUtils.StrToDateFmt ( ‘yyyy/mm/dd’, ‘2017/01/01’ );
[/sourcecode] from the JvJCLUtils.pas file.

Leave a Reply

Your email address will not be published. Required fields are marked *


CAPTCHA Image
Reload Image