I have some HTML file with 2 objects:
<input type='date' id = 'datepicker1'>
<input type='time' id = 'timepicker1'>
I use the .valueAsDate
attribute in order to set the date and time to be the current ones:
document.getElementById("datepicker1").valueAsDate = new Date();
document.getElementById("timepicker1").valueAsDate = new Date();
While the date picker shows today's date perfectly (March 16, 2016), the time picker shows the time 2 hours ago (compared to the computer time). For example, if I load the HTML page in 18:26:32, the time picker will instead show 16:26:32.
It has the same behavior when I use Date.now()
instead of new Date()
Any idea how to solve it?
EDIT:
Here is the solution, it works amazingly (Thanks to @LGSon's): (from link here: Javascript Date() give wrong date off by one hour):
var d = new Date();
document.getElementById("datepicker1").valueAsDate = d.getTime() - d.getTimezoneOffset() * 60 * 1000;