I'm trying to get day of week by :
const date = new Date('2021/12/06')
date.getDay()
and it works properly , but when I create date by using new Date(2021, 12, 6)
, it show me wrong number - 4
. Why is it happens , or I don't understand some exception for this way of creating date object ?
CodePudding user response:
this is because month should be the month index (0 - 11) not the month order (1-12):
new Date(2021, 11, 6).getDay()
and not
new Date(2021, 12, 6).getDay()
REF: Date() constructor: syntax
CodePudding user response:
When you create a date by 3 params you have to use a month INDEX. Starting by 0.
So December is actually 11, not 12.
If you use new Date(2021, 11, 6)
you get the result you're looking for.
CodePudding user response:
getDay() gets the index of the week. Sunday, Monday, Tuesday, etc. (0, 1, 2,3,...)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay
I believe what you're looking for is getDate() https://www.w3schools.com/js/js_date_methods.asp