Solved

Convert a string into a TDateTime

Posted on 2016-10-18
5
71 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 27

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

679 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