JS time format HH:mm
July 31, 2021
Just because I couldn't believe it… obviously there's no easier way in JavaScript to show the time in HH:mm (e.g. 11:05) then to build the string yourself, using if-conditions and adding the zero manually where needed:
<body onload="date()">
<h1>Time format HH:mm</h1>
<p id="time"></p>
<script>
function date() {
var time = new Date();
var hours = time.getHours();
var minutes = time.getMinutes();
hours = hours < 10 ? ('0' + hours) : hours;
minutes = minutes < 10 ? ('0' + minutes) : minutes;
document.getElementById("time").innerHTML = hours + ':' + minutes;
}
</script>
</body>