En este simple html <input type="time" id="tm" name="tm"> , trato de establecer la hora usando document.getElementById('tm').valueAsDate = new Date(); , pero la hora se establece en "23:45:04.123", lo que da como resultado un error en la entrada. Recibo una información sobre herramientas "Seleccione un valor válido..."
Intenté esto en Firefox 94.0.2 y Edge 96.0.1054.34: el mismo resultado. Estoy usando un Windows 10 x64 alemán, si eso importa.
Aquí está mi violín:
Puedes usar el momento
var now = new Date(Date.now()); document.getElementById('tm').valueAsDate = moment(now, ["h:mm A"]).format("HH:mm");Compruebe también estas opciones.
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;usa este código
<input type="time" id="tm" name="tm"> <script> var today = new Date(); var time =today.getHours()+":"+today.getMinutes() ; document.getElementById("tm").value = time; </script>Puede intentar obtener la parte de horas y minutos por separado y unirlos para lograr el formato deseado.
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">