toLocaleTimeString() always showing leading zero

Viewed 615

I would like to display this as something like: 4:26 PM.

But what I keep getting is: 04:26 PM.

If I remove the hour and minute options from toLocaleTimeString(), it works, but it also displays seconds, which I don't want.

myClock();
function myClock() { 
    var d = new Date().toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});   
    document.getElementById("clock").innerHTML = d; 
}
setInterval(myClock, 1000);
<div id="clock"></div>

1 Answers

You're setting the 2-digit format for hours; sounds like you want numeric instead:

myClock();
function myClock() { 
    var d = new Date().toLocaleTimeString([], {hour: 'numeric', minute:'2-digit'});   
    document.getElementById("clock").innerHTML = d; 
}
setInterval(myClock, 1000);
<div id="clock"></div>

Related