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

JavascriptDateMomentjs

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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/

Solution 2 - Javascript

An even easier solution would be to use moment.date(0). the .date() function takes the 1 to n day of the current month, however, passing a zero or negative number will yield a dates in the previous month.

For example if current date is February 3rd:

var _date = moment(); // 2018-02-03 (current day)
var _date2 = moment().date(0) // 2018-01-31 (start of current month minus 1 day)
var _date3 = moment().date(4) // 2018-02-04 (4th day of current month)
var _date4 = moment().date(-4) // 2018-01-27 (start of current month minus 5 days)

console.log(_date.format("YYYY-MM-DD"));
console.log(_date2.format("YYYY-MM-DD"));
console.log(_date3.format("YYYY-MM-DD"));
console.log(_date4.format("YYYY-MM-DD"));

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>

Solution 3 - Javascript

Last month's First date and last month's last date from the current date basis. The Format of the date changes depending upon. (DD-MM-YYYY)

console.log("last month first date");
   const lastmonthlastdate=moment().subtract(1, 'months').startOf('month').format('DD-MM-YYYY')
console.log(lastmonthlastdate);

console.log("lastmonth last date");
   const lastmonthfirstdate=moment().subtract(1, 'months').endOf('month').format('DD-MM-YYYY')
console.log(lastmonthfirstdate);

Solution 4 - Javascript

You can get the first day of the month then subtract 1 day to get the last day of the previous month.

const monthyear = moment().format('YYYY-MM')
const firstDay = moment(monthyear + "-01").format("YYYY-MM-DD");
// Subtract 1 day to get the end of the previous month
const dateTo = moment(firstDay).subtract('1', 'days').format("YYYY-MM-DD");

Solution 5 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionredromView Question on Stackoverflow
Solution 1 - JavascriptMorKadoshView Answer on Stackoverflow
Solution 2 - JavascriptmhodgesView Answer on Stackoverflow
Solution 3 - JavascriptJojo JosephView Answer on Stackoverflow
Solution 4 - JavascriptCeazarMasulaView Answer on Stackoverflow
Solution 5 - JavascriptMike SmitView Answer on Stackoverflow