Clock format in javascript

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?

+6
javascript
source share
7 answers
var hour = GetHour() < 10 ? '0' + GetHour() : GetHour(); 
+6
source share
 var d,h d = new Date() h = (h = d.getHours()) < 10 ? '0' + h : h 
+3
source share

The obvious answer is to use the IF statement ...

 var dat = new Date(); var hr = dat.getHour(); if(hr < 10) { hr = "0" + hr; } 
+1
source share

You can always just do

 var time = new Date(); ('0' + time.getDate()).slice(-2) 
+1
source share

There is no function for this. To add a lead of 0:

use the following:
 var date = new Date(); var hours = new String(date.getHours()); if (hours.length == 1) { hours = "0" + hours; } 
0
source share

If you are using the Prototype framework, toPaddedString which does this.

 a = 2; a.toPaddedString(2) // results in "02" 
0
source share

All Articles