1

It is mentioned in here, that :

Javascript date getDay() method returns the day of the week for the specified date according to local time.

I cannot understand why local time influences the output of that method?

At the moment I'm writing this question, It is

Tuesday,   February 11, 2020  11:49:46 pm in Honolulu, Hawaii, USA and it is  
Wednesday, February 12, 2020  03:48:45 am in Houston, Texas, USA 

(I've got those local times from here)

So it is Tuesday in one region and Wednesday in another

Though one of the regions is still in Tuesday, But the fact is that in both countries, Wednesday is 12th February!

The only difference I can see is that one of those two regions reaches 12th February a bit later!

Hence, regardless of the local time, new Date(2020, 1, 12).getDay() should return 3(Wednesday). Why is it written that output is according to local time?

Shahryar Saljoughi
  • 2,599
  • 22
  • 41
  • Because it is executed in a browser, which reads the time from the machine it is installed on. – Jeremy Thille Feb 12 '20 at 10:02
  • So use UTC if you need – mplungjan Feb 12 '20 at 10:04
  • @JeremyThille I know that Jeremy! I'm trying to say that whatever the local time of my machine is, the output of `new Date(2020, 1, 12).getDay()` is the same! How does it make sense to say that result is according to local time? – Shahryar Saljoughi Feb 12 '20 at 10:11
  • 1
    It says its in local time because `new Date(2020, 1, 12)` is in local time too. Alternatively you could do `new Date(Date.UTC(2020, 1, 12)).getUTCDay();` – Roland Starke Feb 12 '20 at 10:16
  • 1
    Ah, I get it now. You're right, if you give it a specific date, it's not according to local time since it's an absolute value. The _default_ (when no explicit date is passed) is local time. – Jeremy Thille Feb 12 '20 at 10:16

2 Answers2

2

These days you can expect the output to be the same, if you pass in the same date, but in old ages there were a lot of different timezones and it could be that at one point a certain date was at a different day. So that note according to local time is probably only valid for edge cases in history

I can refer you to this answer for more information and some great background information

Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149
  • We still have different time zones. don't we? I'd really appreciate if you explain more how the old ages were different than now. thank you. or may be just introduce some link or reference. – Shahryar Saljoughi Feb 12 '20 at 10:15
  • @ShahryarSaljoughi I've updated my answer with another answer :) – Poul Kruijt Feb 12 '20 at 10:18
1

I cannot understand why local time influences the output of that method?

ECMAScript Date objects are are based on a time value that is an offset from 1970-01-01T00:00:00Z, making them inherently UTC. So for a given time value, the value returned by getDay may be different depending on the host timezone offset.

Hence, regardless of the local time, new Date(2020, 1, 12).getDay() should return 3(Wednesday).

Yes, but note that the values are interpreted as local, so the time value (i.e. the UTC offset) generated on different machines may be different based on the host timezone offset.

Why is it written that output is according to local time?

One method of transferring dates is to send the time value (sometimes also called a "UNIX timestamp") that represents a specific time UTC. Since getDay returns the local day, creating a Date from that time value and using getDay may return a different value depending on the host timezone offset.

RobG
  • 142,382
  • 31
  • 172
  • 209