I created a clock to be placed in the header of my website. The time is not displaying a zero for minutes < 10. For example if the time is 10:50, it will only show 10:5 ,I found a solution but unsure of how to implement it. Also if there is a better method please share.
var current;
window.onload = function () {
current = new Date();
document.getElementById("clock").innerHTML = current.getHours() + ":" + current.getMinutes();
This is what I need
if (minutes < 10)
minutes = "0" + minutes
and this is the container for my clock
<span id="clock"> </span>
I like this way of doing things... Javascript add leading zeroes to date
const d = new Date();
const date = (`0${d.getMinutes()}`).slice(-2);
console.log(date); // 09;
2019 Update: But I now prefer
const d = new Date();
const date = String(d.getMinutes()).padStart(2, '0');
console.log(date); // 09;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With