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

116
Views
Setting HTML input of type time prints invalid input time

In this simple html <input type="time" id="tm" name="tm">, I try to set the time using document.getElementById('tm').valueAsDate = new Date();, but the time is set to "23:45:04.123", which results in an error in the input. I get a tooltip "Please select a valid value...."

I tried this in Firefox 94.0.2 and Edge 96.0.1054.34 - the same result. I'm using a German Windows 10 x64, if that matters.

Here's my fiddle:

https://jsfiddle.net/tg0v43m9/1/

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use moment


var now = new Date(Date.now());
document.getElementById('tm').valueAsDate = moment(now, ["h:mm A"]).format("HH:mm");

Also check these options.

var now = new Date(new Date().toLocaleString("en-US", {
    day: '2-digit',
    month: '2-digit',
    year: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    hour12: true,
}));
document.getElementById('tm').valueAsDate = now;

about 4 years ago · Juan Pablo Isaza Report

0

use this code

<input type="time" id="tm" name="tm">
<script>

    var today = new Date();

    var time =today.getHours()+":"+today.getMinutes() ;
    document.getElementById("tm").value = time;

</script>
about 4 years ago · Juan Pablo Isaza Report

0

You can try to get the hours & minutes part separately and join them together to achieve your desired format.

var now = new Date(Date.now());

// Getting Hours from the time
var hours = now.getHours();

// Getting minutes from the time
var minutes = "0" + now.getMinutes();

// Displaying time in H:M format
var time = hours + ':' + minutes.substr(-2);
  
document.getElementById('tm').value = time; 
// document.getElementById('tm').value = "23:45"; // works
<input type="time" id="tm" name="tm">

about 4 years ago · Juan Pablo Isaza 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!