heyarne
heyarne

Reputation: 1167

Strange date behavior with mongoose

I have a document in my database that gets displayed like this when inspected with MongoDB Compass:

mongodb object

Now I have experimented a bit with it on the command line:

command line experiments

I understand that getMonth returns one because they range from 0 to 11, but why does getDay return numbers that to me appear have no relation whatsoever with the saved date?

Upvotes: 1

Views: 78

Answers (2)

Ivan Vasiljevic
Ivan Vasiljevic

Reputation: 5708

if you look documentation for getDay() you will find out that:

The getDay() method returns the day of the week (from 0 to 6) for the specified date.

I think that on your computer culture week starts on Sunday. So you are getting numbers that you are gettings.

Also I bolive that you wanted to use getDate() method:

The getDate() method returns the day of the month (from 1 to 31) for the specified date.

Upvotes: 1

s7vr
s7vr

Reputation: 75914

getDay() returns the day of the week (from 0-6).

So wednesday will be 2.

Try getDate() to return the day of the month.

Upvotes: 2

Related Questions