In my javascript iam using the GetHour and GetMinutes functions. For example, the current time is 2:03. If in this case I use GetHour (), it returns 2.Instead do I need 02.Can I help someone?
var hour = GetHour() < 10 ? '0' + GetHour() : GetHour();
var d,h d = new Date() h = (h = d.getHours()) < 10 ? '0' + h : h
The obvious answer is to use the IF statement ...
var dat = new Date(); var hr = dat.getHour(); if(hr < 10) { hr = "0" + hr; }
You can always just do
var time = new Date(); ('0' + time.getDate()).slice(-2)
There is no function for this. To add a lead of 0:
var date = new Date(); var hours = new String(date.getHours()); if (hours.length == 1) { hours = "0" + hours; }
There is a javascript library that looks like it will handle all kinds of date conversions, including formatting dates, so the month is always two digits.
If you are using the Prototype framework, toPaddedString which does this.
toPaddedString
a = 2; a.toPaddedString(2) // results in "02"