1

A have a date on a string on the format yyyy-MM-ddTHH:mm:ss

I need to create a javascript Date object, so I do

var d = new Date('2017-07-01T00:38:00')

and the created object is

Sat Jul 01 2017 00:38:00 GMT+0100 (GMT Daylight Time)

This is what I expect and what is happening on Chrome (v 59.0.3071.115 ) on my Desktop.

When I run the same code on my android phone, with Chrome version 51.0.2704.81 the result is

Sat Jul 01 2017 01:38:00 GMT+0100 (GMT Daylight Time)

As you can see the hour is different due to my local timezone.

Is any of the above behaviors an expected one? Is it a matter of Chrome version? On my search I understood that the Date constructor with a string date produces unexpected results but can I rely on constant behavior independently of the browser?


EDIT

It's not a duplicate because on Firefox I have the same result that Chrome v59, the datetime is interpreted as local date and not UTC (that's what I want).

EDIT 2

On the "alleged" duplicated question the 'asker's' objective is that the new Date() treats the inserted date as UTC and make the necessary conversion to the users local time. What I want is that the new Date() process the inserted date as local date without making any conversions. This is the behavior I'm getting on recent browsers (Chrome, Firefox) but no on old version of Chrome (in Android, v51). So, if the users is on a timezone with +01:00 offset, if I make new Date('2017-07-01T00:38:00') the result should be Sat Jul 01 2017 00:38:00 GMT+0100

EDIT 3

Just checked that if I use the constructor new Date(2017,06,01,0,38,0) the date is interpreted as localdate (it shows Sat Jul 01 2017 00:38:00 GMT+0100 (GMT Daylight Time) on both versions of chrome... Is there any documentation that I can rely to be sure that will always interpreted as local date?

NunoRibeiro
  • 511
  • 2
  • 7
  • 22

1 Answers1

0

There is a bug in ECMAScript 5.1 spec.

Chrome implemented what the spec said literally, and replaced the behaviour of Date in Chrome 35 to one you should expect.


On the previous specs (ECMAScript 5.1):

Z is the time zone offset specified as “Z” (for UTC) or either “+” or “-” followed by a time expression HH:mm

[...]

The value of an absent time zone offset is “Z”.

On the current specs (fixed):

When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.

Yam Mesicka
  • 6,243
  • 7
  • 45
  • 64
  • according to your answer, on Chrome 51 this datetime should be interpreted as local time but its interpreted as UTC – NunoRibeiro Jul 01 '17 at 00:37
  • Many would say that ed 5.1 got it right by being consistent with ISO 8601 and that the current spec is wrong. As it is now, it's consistent with ISO 8601 mostly but date-only forms are an exception. – RobG Jul 01 '17 at 21:33