LeTay
asked on
Convert a string into a TDateTime
I know that the StrToDateTime() function uses the locale settings to do the conversion of a string into a TDateTime
In my case, the string contains the date in this format : yyyy-mm-dd
So how can I convert it into TDateTime without locale settings interferences ?
Thanks
In my case, the string contains the date in this format : yyyy-mm-dd
So how can I convert it into TDateTime without locale settings interferences ?
Thanks
Try it this way
var
MySettings: TFormatSettings;
s: string;
d: TDateTime;
begin
GetLocaleFormatSettings(Ge tUserDefau ltLCID, MySettings);
MySettings.DateSeparator := '-';
MySettings.TimeSeparator := ':';
MySettings.ShortDateFormat := 'yyyy-mm-dd';
MySettings.ShortTimeFormat := 'hh:nn:ss';
s := DateTimeToStr(Now, MySettings);
ShowMessage(s);
d := StrToDateTime(s, MySettings);
ShowMessage(DateTimeToStr( d, MySettings));
end;
Adapted from
http://www.distribucon.com/2006/01/10/DelphiStrToDateTimeWithFormatSettings.aspx
mlmcc
var
MySettings: TFormatSettings;
s: string;
d: TDateTime;
begin
GetLocaleFormatSettings(Ge
MySettings.DateSeparator := '-';
MySettings.TimeSeparator := ':';
MySettings.ShortDateFormat
MySettings.ShortTimeFormat
s := DateTimeToStr(Now, MySettings);
ShowMessage(s);
d := StrToDateTime(s, MySettings);
ShowMessage(DateTimeToStr(
end;
Adapted from
http://www.distribucon.com/2006/01/10/DelphiStrToDateTimeWithFormatSettings.aspx
mlmcc
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
There's an overload version of StrToDateTime() in SysUtils that let you pass your FormatSettings
In example
In example
procedure TForm1.Button1Click(Sender: TObject);
var
F: TFormatSettings;
DT: TDateTime;
begin
F.DateSeparator := '-';
F.ShortDateFormat := 'yyyy-mm-dd';
DT := StrToDateTime('2016-10-19', F);
ShowMessage(DateTostr(DT));
end;
ASKER
Simple is beautiful !
ex:
DateString := FormatDateTIme('mm/dd/yyyy