Solved

Convert a string into a TDateTime

Posted on 2016-10-18
5
63 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 100

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 26

Accepted Solution

by:
Sinisa Vuk earned 500 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 22

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 327
CheckListBox usage 3 71
Sending files from  idTcpServer Socket to idTcpClient 2 44
How to make Sign in, using Clientdataset? 1 28
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

837 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