View Full Version : time in 2 digits
aEr_aEr
12-18-2002, 12:04 PM
If i do Date.getMinutes then i get 1 for example but i want to get 01.
Is there somekind of method to do this like in Number.toFixed(2) but then before the decimal point
glenngv
12-18-2002, 12:06 PM
var today = new Date();
var min = today.getMinutes();
if (min<10) min="0"+min;
alert(min);
aEr_aEr
12-18-2002, 12:07 PM
ofcourse
thanks
Zvona
12-18-2002, 01:11 PM
I once made a function for this purpose :
function formatNumber(iValue,iPre)
{
for (var iI=iPre-1;iI>0;iI--)
{
if (iValue < Math.pow(10,iI))
iValue = "0" + iValue;
}
return iValue;
}
For instance :
var dtNow = new Date();
var iHour = dtNow.getHours();
var sHour = formatNumber(iHour,2);
var sHour2 = formatNumber(iHour,4);
When iHour is "8", sHour is "08" and sHour2 is "0008".
beetle
12-18-2002, 04:44 PM
Ya, I used something simple with my date object...function leadingZero(num) {
return (num < 10) ? "0" + num : num.toString();
}
whammy
12-19-2002, 02:00 AM
I like that, beetle, that returns exactly the same thing as this example in server-side ASP/VBScript:
hours = Right("00" & hours,2)
I use that kind of thing all the time... I should make a "LeadingZeroes" function that gets the number of leading zeroes needed from the function reference. I'll add that to my functions.asp script sometime this week.
:)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.