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:
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;
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>
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">