0
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)); //Output:Fri Dec 31 1869 05:53:20 GMT+0553 (India Standard Time)

var date3 = "1870-01-01";
console.log(new Date(date3)); //Output:Sat Jan 01 1870 05:21:10 GMT+0521 (India Standard Time)

When debugging some Date() related issues, saw the above behavior of Date() with different inputs.

The time zone is different for all the three inputs even though my System time zone is GMT +05:30

Is this expected?

Thanks for any help in advance!

Senthil
  • 43
  • 1
  • 10
  • Yes. What did *you* expect? – deceze Jul 21 '21 at 14:16
  • @deceze The time zone is different for all the 3 inputs. Isn't that supposed to be +05:30 always(my local system time is GMT +05:30). – Senthil Jul 21 '21 at 14:18
  • 3
    The timezone +05:30 didn't exist back then. https://en.wikipedia.org/wiki/Time_in_India#Former_timezones – deceze Jul 21 '21 at 14:21
  • @deceze Even then shouldn't that be consistent? Within a day's difference, the time zone shifted by almost 30mins – Senthil Jul 21 '21 at 14:28
  • 1
    Then that's a date at which a different timezone was introduced in the country. – deceze Jul 21 '21 at 14:29
  • 1
    Your current system time zone only matters for current time. For past and future dates, JavaScript will use the appropriate time zone that was active at the time, gathering the information from the engine's IANA time zone database. For reference, my own country swaps time zone twice every year to comply with daylight saving time. – Álvaro González Jul 21 '21 at 14:34
  • @deceze +05:53 time zone was introduced in 1884 but I'm getting it for the dates before 1869. Similarly, +05:21 time zone was introduced in 1804, but I'm getting it for the dates within 1870 - 1905. From 1906 onwards its returning +05:30. – Senthil Jul 21 '21 at 14:47
  • @ÁlvaroGonzález Thanks for the explanation, will check in this direction. – Senthil Jul 21 '21 at 14:51

1 Answers1

4

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.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575