Reputation: 215
How do I set format on time? It shows 16:9:2 It should show 16:09:02
my code:
setInterval(function _timer() {
var time = new Date();
var h = time.getHours();
var m = time.getMinutes();
var s = time.getSeconds();
var fullTime = h + ":" + m + ":" + s;
fullTime.format("hh-MM-ss");// this is not working
$("#time").html(fullTime);
1000
});
Upvotes: 3
Views: 2132
Reputation: 2884
If you're able to use an external dependency, the momentjs library is one of the best out there!
You could replace all your formatting with:
var now = moment().format("HH:mm:ss");
Upvotes: 4
Reputation: 172418
You can try like this:
var time = new Date();
var h = time.getHours();
var m = time.getMinutes();
var s = time.getSeconds();
alert(
("0" + h).slice(-2) + ":" +
("0" + m).slice(-2) + ":" +
("0" + s).slice(-2));
Upvotes: 4