Recent

Author Topic: Does FormateDateTime work this way?  (Read 785 times)

nugax

  • Full Member
  • ***
  • Posts: 232
Does FormateDateTime work this way?
« on: April 07, 2022, 04:11:47 am »
I am using FormatDateTime - trying to get hours/mins with lead zeros. According to the formatchars it should be 'hh' and 'nn' and 'ss' but the output doesnt have the lead zero. What am I doing wrong?

Code: Pascal  [Select][+][-]
  1.   curYear := FormatDateTime('yyyy', NOW);
  2.   curMon := FormatDateTime('mm', NOW);
  3.   curDay := FormatDateTime('dd', NOW);
  4.   curHour := FormatDateTime('hh', NOW);
  5.   curMin := FormatDateTime('nn', NOW);
  6.   curSec := FormatDateTime('ss', NOW);


What I get:

Code: Pascal  [Select][+][-]
  1. Date           : 2022/4/6 @ 21:7
  2.  
-Nugax

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: Does FormateDateTime work this way?
« Reply #1 on: April 07, 2022, 04:49:20 am »
not that I know off. It might return the result as a string but I'm guessing you are after something like.
Code: Pascal  [Select][+][-]
  1. Interface
  2. uses DateUtils ......;
  3.  
  4. procedure DecodeDateTime(const aValue:TDateTime; var Year, Month, DayOfMonth, Hour, Min, Sec, mSec:Word);
  5.  
  6. Implementation
  7.  
  8. procedure DecodeDateTime(const aValue:TDateTime; var Year, Month, DayOfMonth, Hour, Min, Sec, mSec:Word);
  9. begin
  10.   DecodeTime(aValue,Hour,Min,Sec,mSec);
  11.   DecodeDate(aValue,Year, Month, DayOfMonth);
  12. end;
  13.  
  14.  

example usage;


Code: Pascal  [Select][+][-]
  1. var
  2.   curYear, curMon, curDay, curHour, curMin, curSec, curMiliSecond :word;
  3. begin
  4.   DecodeDateTime(Now, curYear, curMon, curDay, curHour, curMin, curSec, curMiliSecond);
  5.   ShowMessage(Format('Year : %D, Month: %D, Day: %D, Hour: %D, Minute: %D, Second: %D, mSecond: %D',[curYear, curMon, curDay, curHour, curMin, curSec, curMiliSecond]));
  6. end;
  7.  

Be aware, the code was typed directly in the browser and hasn't been tested.

bytebites

  • Hero Member
  • *****
  • Posts: 640
Re: Does FormateDateTime work this way?
« Reply #2 on: April 07, 2022, 04:59:23 am »
You are doing wrong, that you give only code partial code.

Code: Pascal  [Select][+][-]
  1. writeln(FormatDateTime('dd/mm/yy hh:nn:ss',44658.25011));  

Prints leading zeros.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Does FormateDateTime work this way?
« Reply #3 on: April 07, 2022, 08:58:36 am »
I am using FormatDateTime - trying to get hours/mins with lead zeros. According to the formatchars it should be 'hh' and 'nn' and 'ss' but the output doesnt have the lead zero. What am I doing wrong?

Please provide a full example. The following outputs leading zeros without problems:

Code: Pascal  [Select][+][-]
  1. program ttest;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   SysUtils, DateUtils;
  7.  
  8. var
  9.   curYear, curMon, curDay, curHour, curMin, curSec: String;
  10. begin
  11.   curYear := FormatDateTime('yyyy', NOW);
  12.   curMon := FormatDateTime('mm', NOW);
  13.   curDay := FormatDateTime('dd', NOW);
  14.   curHour := FormatDateTime('hh', NOW);
  15.   curMin := FormatDateTime('nn', NOW);
  16.   curSec := FormatDateTime('ss', NOW);
  17.  
  18.   Writeln(curYear, '/', curMon, '/', curDay, ' @ ', curHour, ':', curMin, ':', curSec);
  19. end.

Output:

Code: [Select]
PS D:\fpc\git> .\testoutput\ttest.exe
2022/04/07 @ 08:56:29

Though as bytebites wrote the normal intention of FormatDateTime is to include the separators there as well.

AlexTP

  • Hero Member
  • *****
  • Posts: 2402
    • UVviewsoft
Re: Does FormateDateTime work this way?
« Reply #4 on: April 07, 2022, 09:05:31 am »
Confirmed the normal work on the demo above, and even with separators:

FormatDateTime('yyyy.mm.dd, hh:nn:ss', NOW);

outputs numbers with lead-zeroes.


nugax

  • Full Member
  • ***
  • Posts: 232
Re: Does FormateDateTime work this way?
« Reply #5 on: April 07, 2022, 03:02:10 pm »
I was using it separately because I need each field separated into a var... hours, mins, secs, etc.
-Nugax

AlexTP

  • Hero Member
  • *****
  • Posts: 2402
    • UVviewsoft
Re: Does FormateDateTime work this way?
« Reply #6 on: April 07, 2022, 03:15:17 pm »
Please provide a full example.

 

TinyPortal © 2005-2018