0

I am trying to show the next whole minute in a clock. When I increment the min with min++ the display shows 60 mins then changes the hour 1 second later.

How do I change it?

function getClockTime_2() {
  let date = new Date();

  let hr = date.getHours();
  let min = date.getMinutes();
  min++ // causes the min to go to 60 and change the hour one second later.

  hr = ("0" + hr).slice(-2);
  min = ("0" + min).slice(-2);
  
  let clock24 = document.getElementById("clockDiv2");
  
  clock24.innerHTML = `${hr}:${min}:00`;
  
}
setInterval(getClockTime_2, 1000);
kmoser
  • 8,780
  • 3
  • 24
  • 40

2 Answers2

0

Use setMinutes of Date.

You can increase it before get minute instead of increase manual.

function getClockTime_2() {
  let date = new Date();
  
  date.setMinutes(date.getMinutes() + 1)
  let hr = date.getHours();
  let min = date.getMinutes();

  hr = ("0" + hr).slice(-2);
  min = ("0" + min).slice(-2);
  
  let clock24 = document.getElementById("clockDiv2");
  
  clock24.innerHTML = `${hr}:${min}:00`;
  
}
<div id="clockDiv2">00:00:00</div>

<button onclick="getClockTime_2()">Click me</button>
hong4rc
  • 3,999
  • 4
  • 21
  • 40
0
//Get the current time

function getTime() { let todaDate = new Date();

todaDate.setMinutes(todaDate.getMinutes() + 1);
let hour = todaDate.getHours();
let minute = todaDate.getMinutes();
let second = todaDate.getSeconds();

hour = ("0" + hour).slice(-2);
minute = ("0" + minute).slice(-2);
second = ("0" + second).slice(-2);


let clock = hour + ":" + minute + ":" + second;

return clock;

}

//Set current time in HTML before call setInterval method.

function clockTime() {

str = getTime();
document.getElementById("clockDiv2").innerHTML = str;

}

function refreshTime() {

str = getTime();
document.getElementById("clockDiv2").innerHTML = str;

}

setInterval(refreshTime, 1000);

clockTime();

BhattMeet
  • 11
  • 3