Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert a string into a TDateTime

Posted on 2016-10-18
5
Medium Priority
?
110 Views
Last Modified: 2016-10-19
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
0
Comment
Question by:LeTay
5 Comments
 
LVL 14

Expert Comment

by:SteveBay
ID: 41848686
The function you need is FormatDateTime
ex:
DateString := FormatDateTIme('mm/dd/yyyyy', Now );
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 41848735
Try it this way


var
  MySettings: TFormatSettings;
  s: string;
  d: TDateTime;
begin
  GetLocaleFormatSettings(GetUserDefaultLCID, 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
0
 
LVL 28

Accepted Solution

by:
Sinisa Vuk earned 2000 total points
ID: 41849131
For older version of Delphi (<XE)....
function MyStrToDateTime(s: String): TDateTime;
var
  yy,mm,dd: Word;
begin
  yy := StrToInt(Copy(s, 1, 4));
  mm := StrToInt(Copy(s, 6, 2));
  dd := StrToInt(Copy(s, 9, 2));
  Result := EncodeDate(yy, mm, dd);
end;

Open in new window

0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 41849561
There's an overload version of StrToDateTime() in SysUtils that let you pass your FormatSettings
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;

Open in new window

0
 

Author Closing Comment

by:LeTay
ID: 41849607
Simple is beautiful !
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Integration Management Part 2
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question