Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert date and time in different formats?

Data I am having

I have a data stored in database which has a date and time stored as a string.

This data is stored at the central server. In this data the date and time has been stored in different formats (varies as per the client machines date time format).

I wanted to do

Now, I want to convert these all different formatted date and time strings to "yyyy-mm-dd hh:mm:ss" format.

Problem I am facing

I am not able to convert date-time data with multiple date time format to a single format.

Example: My machine date time format is yyyy-mm-dd. but when I am trying to convert the date below

StrToDateTime('2015/02/10')

It throws an exception.

Can any one tell me how I can achieve that? Please let me know if any more details are required. Thank you in advance.

like image 633
A B Avatar asked Oct 23 '25 19:10

A B


1 Answers

You need custom date format settings, like that:

procedure Test;
var
  DT: TDateTime;
  FS: TFormatSettings;

begin
  FS:= TFormatSettings.Create;
  FS.DateSeparator:= '/';
  FS.ShortDateFormat:= 'yy/m/d';
  DT:= StrToDateTime('2015/02/10', FS);
  Writeln(FormatDateTime('dd mm yyyy', DT));
end;
like image 92
kludg Avatar answered Oct 26 '25 10:10

kludg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!