Home > OS >  input "datetime-local" > js code that makes the seconds run/move accurately for time to
input "datetime-local" > js code that makes the seconds run/move accurately for time to

Time:12-11

I would like to find out how to have the seconds run/move accurately for the time to change. is there a way I can do that with the js script that I have provided below?

thank you all.

functionName(); //First execution
 setInterval(functionName, 1000); //Ask the browser to execute functionName as soon as possible after 1 second has passed

function functionName(){
   var now = new Date();
   now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
   document.getElementById('dt').value = now.toISOString().slice(0,16);
}
<input readonly="readonly" name="dt" id="dt" type="datetime-local" step="1" />

CodePudding user response:

Using getSeconds and setSeconds works:

I also changed the string slice to .slice(0,19) to show the updated seconds.

functionName(); //First execution
 setInterval(functionName, 1000); //Ask the browser to execute functionName as soon as possible after 1 second has passed

function functionName(){
   var now = new Date();
   now.setSeconds(now.getSeconds() - now.getTimezoneOffset());
   document.getElementById('dt').value = now.toISOString().slice(0,19);
}
<input readonly="readonly" name="dt" id="dt" type="datetime-local" step="1" />

  • Related