Solved

Convert a string into a TDateTime

Posted on 2016-10-18
5
45 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 25

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Splash screen too shortlived 11 113
PDF library for Delphi 2 89
Machine not responding during CopyFile() 3 79
delphi prevent click fast 2 166
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now