Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 datetime-local (Chrome): How to input datetime in current time zone

Tags:

html

timezone

I live in GMT+9, I want to input date and time that specified in my current time zone. Chrome Version 40.0.2214.93 (64-bit)

<input type="datetime-local" required>

However, if I input 12:00AM in the datetime picker,

enter image description here

the actual value that the element returns is 2015-01-30T00:00 (by printing out in the Chrome console), which equals 9:00AM in my time zone. Do I have to manually add 9 hours to the result? Since Chrome doesn't support datetime as an input type, I cannot use datetime as a replacement.

like image 749
shapeare Avatar asked Jan 30 '15 11:01

shapeare


1 Answers

"2015-01-30T00:00" is exactly what you entered, so that is the result that is given back.

You said "... which equals 9:00AM in my time zone". That would only be true if the result was in UTC - which it is not. By the ISO8601 standard, it would only be presumed to be in UTC if the value ended with a trailing Z, such as "2015-01-30T00:00Z".

Perhaps you are feeding the value into the Date constructor?

new Date("2015-01-30T00:00")

In that case, the value will be interpreted as UTC - but that's due to a quirk of the Date object. You can work around this with a bit of substitution:

new Date("2015-01-30T00:00".replace('T',' ').replace('-','/'))

Or, if you prefer cleaner code, consider using moment.js, which doesn't have that quirk:

moment("2015-01-30T00:00").toDate();  // or .format(), or other functions...
like image 71
Matt Johnson-Pint Avatar answered Nov 15 '22 10:11

Matt Johnson-Pint