+ 1
Can someone help me display the time in JavaScript plz
I'm trying to display the time but it keeps coming out like this 9:23:6 instead of like this 09:23:06 Here's the code: function printTime() { var d = new Date(); var hours = d.getHours(); var mins = d.getMinutes(); var secs = d.getSeconds(); document.body.innerHTML = hours+":"+mins+":"+secs; } setInterval(printTime, 1000)
3 Respuestas
+ 3
As simple as that
function currentTime();
{
let date = new Date();
let hour = date.getHours();
let minute = date.getMinutes();
let second = date.getSeconds();
document.body.innerHTML = hour+":"+minute+":"+second;
}
setInterval(currentTime, 1000);
+ 2
This is the right way bro
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
// add a zero in front of numbers<10
m = checkTime(m);
s = checkTime(s); document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
t = setTimeout(function() {
startTime()
}, 500);
0
Just use
parseInt(secs)
to show second or minute