Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda