jhamm
jhamm

Reputation: 25032

Why is this momentjs date incorrect?

Here is my code:

var moment = require("moment"); 

var day = 31;
var month = 12;
var year = 2016;
moment().date(day).month(month - 1).year(year)

The date that is returned is Sat Dec 03 2016 16:23:43 GMT-0700 (MST).

Why is the date being converted to 03 instead of 31?

Upvotes: 1

Views: 44

Answers (1)

000
000

Reputation: 27227

This line is processed in multiple steps: moment().date(day).month(month - 1).year(year)

First: moment().date(31)

It is currently February 7th, 2017. We are changing it to "February 31st, 2017", which wraps around to March 3rd since February has only 28 days.

Then it changes the month to 12, and the year to 2016.

Flip the steps around. Do the year first, then month, then date.

Upvotes: 1

Related Questions