Recent

Author Topic: How to format date  (Read 1021 times)

Iam2018

  • Newbie
  • Posts: 2
How to format date
« on: May 23, 2020, 05:19:04 pm »
Hi, i'm trying to format a date with slashes but I can't. I can use any separator exept slashes
 
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. begin
  3.   Label1.Caption:='command: DateTimeToStr(Now)  result: ' + DateTimeToStr(Now);                                                     // result:   23-5-20 16:56:43
  4.   Label2.Caption:='command: FormatDateTime(''dd/mm/yyyy hh/mm/ss'',Now)  result: '+ FormatDateTime('dd/mm/yyyy hh/mm/ss',Now);      // result:   23-05-2020 16-56-43
  5.   Label3.Caption:='command: FormatDateTime(''ddqmmqyyyy hhtmmtss'',Now)  result: '+ FormatDateTime('ddqmmqyyyy hhpmmpss',Now);      // result:   23Q05Q2020 16P56P43
  6.   Label4.Caption:='command: FormatDateTime(''dd##mm##yyyy  hh#mm#ss'',Now)  result: '+ FormatDateTime('dd##mm##yyyy  hh#mm#ss',Now);// result:   23##05##2020 16#56#43
  7. end;

Is it a bug of FormatDateTime?

Thanks

TRon

  • Hero Member
  • *****
  • Posts: 2515
Re: How to format date
« Reply #1 on: May 23, 2020, 05:23:59 pm »
Hi, i'm trying to format a date with slashes but I can't. I can use any separator exept slashes
no, it is not a bug :-)

The slash is being interpreted by the FormatDateTime routine as a formatting character, you expected it to be literal.

Whenever this routine (and accompanied routines) detect a special formatting character, then that formatting character will be replaced as described in the link given below.

How exactly the replacement turns out in the end depends on your (global) system-settings. It can have a different outcome depending on the end-user system settings. It is locale dependent.

Try to put your slashes between quotes.

See: https://www.freepascal.org/docs-html/rtl/sysutils/formatchars.html
« Last Edit: May 23, 2020, 05:50:13 pm by TRon »

Iam2018

  • Newbie
  • Posts: 2
Re: How to format date
« Reply #2 on: May 23, 2020, 05:59:10 pm »
Ok.
This is the correct code.
Code: Pascal  [Select][+][-]
  1. FormatDateTime('dd"/"mm"/"yyyy hh"/"mm"/"ss',Now)

and now its result is  23/05/2020 17/55/02.

Thank you very much TRon.

 

TinyPortal © 2005-2018