Skip to content
Advertisement

MomentJS – How to get last day of previous month from date?

I’m trying to get last day of the previous month using:

 var dateFrom = moment(dateFrom).subtract(1, 'months').format('YYYY-MM-DD');

Where:

dateFrom = 2014-11-30 

But after using

subtract(1, 'months')

it returns date

DATE_FROM: "2014-10-30"

But last day of the 10’th month is 31.

How can I solve i please?

Many thanks for any help.

Advertisement

Answer

Simply add a endOf('month') to your calls:

var dateFrom = moment(dateFrom).subtract(1,'months').endOf('month').format('YYYY-MM-DD');

http://jsfiddle.net/r42jg/327/

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement