Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
input "datetime-local" > js code that can update the current time automatically without having to click and select

i am trying to create a script that can update the current time automatically in the datetime-local input without having to click and selecting the current time. this is the code that i am working with to have the input display the current date and time, but i also want it to update the current time when the time changes.

then i hope to submit the datetime-local value to the database using PHP (i got this section covered)

and i was hoping if it's possible to delete the date and time selection button in the input and how can i show the seconds, but have the seconds running accurately with current time

thank you all.

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

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

I think you can set an interval every second for this.

 functionName(); //First execution
 setInterval(functionName, 1000); //Check every second

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

Of course you will need to "Stop" this interval when the user clicks on the input, or the date can never be changed

EDIT: For avoid the user to change the date, just add readonly attribute to the input tag.

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!