onmyway133
onmyway133

Reputation: 48205

Date.getDay function returns wrong value

I've read that https://www.w3schools.com/jsref/jsref_getday.asp returns the day of the week (from 0 to 6) for the specified date. And the index is that Sunday is 0, Monday is 1, and so on. I want to get the day of the week.

But when I try

const date = new Date(2018, 11, 9, 10, 33, 30)
date.getDay()

The date is friday, which should have index 5, but getDay returns 0. This happens in Chrome console and in my React Native apps.

Update: Look at the answers. I was about to specify November 9, 2018. But JavaScript counts months from 0 to 11. So I should use index 10 for November.

Upvotes: 0

Views: 154

Answers (2)

Kokogino
Kokogino

Reputation: 1076

The months start with 0 (January) and end with 11 (December). With new Date(2018, 11, 9, 10, 33, 30) you are one month off.

new Date(2018, 10, 9, 10, 33, 30)

This is what you want.

Upvotes: 2

Jivings
Jivings

Reputation: 23262

Let's try checking that date...

new Date(2018, 11, 9, 10, 33, 30)
> Sun Dec 09 2018 10:33:30 

Looks like it's December 9th 2018, which will be a Sunday :)

Upvotes: 1

Related Questions