When using <input type="time">
and setting the time with valueAsDate
the browser displays the time in GMT instead of local time, is there any way to change this behavior? I want the browser to display the local time.
If you’re not on GMT time than you should see that the hour in the input is different the in the p
element
(I tried this in Chrome and Firefox)
JavaScript
x
3
1
let date = new Date;
2
document.querySelector("input").valueAsDate = date;
3
document.querySelector("p").innerText = date;
JavaScript
1
2
1
<input type="time">
2
<p></p>
Advertisement
Answer
.toLocaleString
can help out here:
JavaScript
1
7
1
let date = new Date;
2
document.querySelector("input").value = date.toLocaleString("sv-SE", {
3
hour: "2-digit",
4
minute: "2-digit",
5
second: "2-digit"
6
});
7
document.querySelector("p").innerText = date;
JavaScript
1
2
1
<input type="time">
2
<p></p>
I wrote up a little article about this problem on dev.to.