Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda