Forum > Beginners
Does FormateDateTime work this way?
nugax:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- curYear := FormatDateTime('yyyy', NOW); curMon := FormatDateTime('mm', NOW); curDay := FormatDateTime('dd', NOW); curHour := FormatDateTime('hh', NOW); curMin := FormatDateTime('nn', NOW); curSec := FormatDateTime('ss', NOW);
What I get:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Date : 2022/4/6 @ 21:7
HeavyUser:
not that I know off. It might return the result as a string but I'm guessing you are after something like.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Interfaceuses DateUtils ......; procedure DecodeDateTime(const aValue:TDateTime; var Year, Month, DayOfMonth, Hour, Min, Sec, mSec:Word); Implementation procedure DecodeDateTime(const aValue:TDateTime; var Year, Month, DayOfMonth, Hour, Min, Sec, mSec:Word);begin DecodeTime(aValue,Hour,Min,Sec,mSec); DecodeDate(aValue,Year, Month, DayOfMonth);end;
example usage;
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var curYear, curMon, curDay, curHour, curMin, curSec, curMiliSecond :word;begin DecodeDateTime(Now, curYear, curMon, curDay, curHour, curMin, curSec, curMiliSecond); ShowMessage(Format('Year : %D, Month: %D, Day: %D, Hour: %D, Minute: %D, Second: %D, mSecond: %D',[curYear, curMon, curDay, curHour, curMin, curSec, curMiliSecond]));end;
Be aware, the code was typed directly in the browser and hasn't been tested.
bytebites:
You are doing wrong, that you give only code partial code.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---writeln(FormatDateTime('dd/mm/yy hh:nn:ss',44658.25011));
Prints leading zeros.
PascalDragon:
--- Quote from: nugax 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?
--- End quote ---
Please provide a full example. The following outputs leading zeros without problems:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program ttest; {$mode objfpc}{$H+} uses SysUtils, DateUtils; var curYear, curMon, curDay, curHour, curMin, curSec: String;begin curYear := FormatDateTime('yyyy', NOW); curMon := FormatDateTime('mm', NOW); curDay := FormatDateTime('dd', NOW); curHour := FormatDateTime('hh', NOW); curMin := FormatDateTime('nn', NOW); curSec := FormatDateTime('ss', NOW); Writeln(curYear, '/', curMon, '/', curDay, ' @ ', curHour, ':', curMin, ':', curSec);end.
Output:
--- Code: ---PS D:\fpc\git> .\testoutput\ttest.exe
2022/04/07 @ 08:56:29
--- End code ---
Though as bytebites wrote the normal intention of FormatDateTime is to include the separators there as well.
AlexTP:
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.
Navigation
[0] Message Index
[#] Next page