Date.getDay () returns different values

I feel that something is missing here.

The Date.getDay () method must return a value between 0 and 6. 0 for Sunday and 6 for Saturday.

Now I have two dates, both Sunday, which should return 0.

new Date('1990-11-11').getDay() // returns 6 new Date('2016-1-3').getDay() // returns 0 

What causes the mismatch? I dare to question the validity of the .getDay() method, but I cannot understand what is happening.

EDIT

 > new Date('1990-11-11') Sat Nov 10 1990 17:00:00 GMT-0700 (MST) > new Date('2016-01-03') Sat Jan 02 2016 17:00:00 GMT-0700 (MST) > new Date('2016-1-3') // they say this format is wrong, but it returns the right date Sun Jan 03 2016 00:00:00 GMT-0700 (MST) 

I do not understand what is going on. January 3 - Sunday and November 11, 1990 - Sunday. Why is he talking on Saturday?

+6
source share
3 answers

Of course, your statement that 1990-11-11 is Sunday is true, but you must understand that the JavaScript Date object is:

  • Time and Date Management
  • Is the time zone known
  • Poorly designed and rather controversial.

Your own tests illustrate this:

 new Date('1990-11-11').getDay() // returns 6 > new Date('1990-11-11') Sat Nov 10 1990 17:00:00 GMT-0700 (MST) 

What happens is that the constructor assumes local time or UTC, depending on the syntax used :

Note. If Date is called as a constructor with more than one argument, the specified arguments represent local time. If UTC is desirable to use a new date (Date.UTC (...)) with the same arguments.

Note: parsing date strings using a date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies. Support for the RFC 2822 line format is by convention only. Support for ISO 8601 formats differs these date strings (for example, "1970-01-01") are treated as UTC, not local.

... and your syntax does it like UTC. But many other methods suggest local time:

The getDay () method returns the day of the week for the specified date according to local time, where 0 represents Sunday.

+1
source

The one who is wrong, the one that returns Sunday, and this must be due to the wrong format. 1990-11-11 interpreted as 00:00:00 at midnight on the 11th, UTC, which is 5pm on Saturday the 10th in your time zone.

If you use getUTCDay() , you should get 0 for both dates.

 new Date('1990-11-11').getUTCDay() // returns 0 new Date('2016-01-03').getUTCDay() // returns 0 
+3
source

getDay returns the index of the day (from 0 to 6), where 0 is Sunday. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay

Return value: An integer corresponding to the day of the week for a given date, according to local time: 0 for Sunday, 1 for Monday, 2 for Tuesday, etc.

Update: the new date constructor returns different time values ​​for these dates.

new Date('2016-1-3') ==> Sun Jan 03 2016 00:00:00 GMT+0100 (CET)

new Date('1990-11-11') ==> Sun Nov 11 1990 01:00:00 GMT+0100 (CET)

And for some reason, the first is interpreted as Saturday on your machine. Sorry for not being able to help more

Update2:

Using two digits for the month / day should standardize the results. Example:

(new Date('2016-01-03')).getDay() ==> 0

getDay

0
source

Source: https://habr.com/ru/post/1013568/


All Articles