Skip to content Skip to sidebar Skip to footer

New Date() Returning Different Values For Time Zones, With Different Inputs

var date1 = '2015-03-29'; console.log(new Date(date1)); //Output:Sun Mar 29 2015 05:30:00 GMT+0530 (India Standard Time) var date2 = '1869-12-31'; console.log(new Date(date2)); //

Solution 1:

The values you are showing are correct.

ECMAScript requires date-only values in YYYY-MM-DD format to be treated as UTC. From the spec:

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

Then the toString function converts to a string representation of local time, which takes the system time zone into account.

The IANA time zone data for India, including rich documentation comments, can be found here. The data itself is as follows (as of tzdata version 2021a):

# Zone  NAME            STDOFF    RULES    FORMAT    [UNTIL]
Zone    Asia/Kolkata    5:53:28   -        LMT       1854 Jun 28  # Kolkata
                        5:53:20   -        HMT       1870         # Howrah Mean Time?
                        5:21:10   -        MMT       1906 Jan  1  # Madras local time
                        5:30      -        IST       1941 Oct
                        5:30      1:00     +0630     1942 May 15
                        5:30      -        IST       1942 Sep
                        5:30      1:00     +0630     1945 Oct 15
                        5:30      -        IST

You can see two of the earlier offsets from your example in the second and third lines of data. If you choose an even earlier date (before 1854-06-28), you'll get the offset from the first line. You can also see that India used a +06:30 offset in two separate periods of history.

All of the history is also available in a more human-readable form at timeanddate.com here.

This is how time zones work. They are not fixed to a single numeric offset, but instead an offset applies to a time zone at a particular point in time, and that offset can change at the whim of the government which controls that time zone. For more on this, read the section titled "Time Zone != Offset" in the timezone tag wiki.


Post a Comment for "New Date() Returning Different Values For Time Zones, With Different Inputs"