221b
221b

Reputation: 431

Moment.js - Incorrect endof date using subtract method

I'm using the moment.js library to plot some values on a graph. I have two buttons called Previous and Next that go one month behind and one month forward from the current date respectively.

Now, the issue that I am facing is with the subtract function - for example, since today is February - the endOf('month') function moves up to 28 days. Now, if I wish to move a month back to January, I am using the following :

moment().endOf('month').subtract(1,'month'+ 's');

This does move back to January, but the date endOf date is still 28 - and due to this my graph only plots until this day of January.

Is there a way I can check for the month and set in the correct last date as I move behind or forward using the subtract function? Or am I missing out on something?

Here is a simple fiddle - I'm alerting the value after using the moment functions.

Upvotes: 0

Views: 339

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49803

Subtract 1 month before using endOf.

Upvotes: 1

Related Questions