0

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;
Community
  • 1
  • 1
SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
  • I'll make it more clear also in the question itself - it shows 2 hours ago comparing to the time of the computer – SomethingSomething Mar 16 '16 at 16:30
  • You can get the date as UTC instead and that should return the correct time... – Neil Hibbert Mar 16 '16 at 16:31
  • 1
    note: since there is no timezone setting for the input, the time input seems to default to GMT when setting from a Date object: https://www.w3.org/TR/html-markup/input.time.html . The time is actually correct, it's just for another timezone. set with a string to align user expectations. – dandavis Mar 16 '16 at 16:36
  • It seems that `Date.now()` does not return the current local time, but the UTC time. How can I convert it to the local time? – SomethingSomething Mar 16 '16 at 16:39
  • 2
    http://stackoverflow.com/questions/32469269/javascript-date-give-wrong-date-off-by-one-hour – Asons Mar 16 '16 at 16:39
  • Date.now() is the local time which is the same as UTC. all the clocks are the same. Time matters _where_ you are asking, not just _when_ you are asking. That said, `new Date().toLocaleString()`, `new Date().toLocaleDateString()`, and `new Date().toLocaleTimeString()` can be used, but DON'T rely on a specific format of those strings, they can vary among browsers and local settings. – dandavis Mar 16 '16 at 16:39
  • @SomethingSomething I completely missed it was tagged java :) .. deleted it – Asons Mar 16 '16 at 16:41
  • a good way to get local date parts: `new Date( Date.now() - (1000*60*new Date().getTimezoneOffset())).toISOString().split(/[\WT]/);` ... yeah, it takes 3 Date calls, but it's still semi-simple... you can substitute another date for the `Date.now()` part to format a specific date instead of the current one. – dandavis Mar 16 '16 at 16:44
  • @LGSon, your link solved my problem. Thanks!!! – SomethingSomething Mar 16 '16 at 16:47
  • @dandavis—the alternative is to create a date, then use *getHours*, *getMinutes* and *getSeconds*. Of course you can always add custom *getLocalTime* and *getUTCTime* methods that return formatted time strings. ;-) – RobG Mar 17 '16 at 03:25

0 Answers0