Enter the month of the month in the last month using the moment

I am using the following code to get the startDate and endDate of recent months.

// Previous month var startDateMonthMinusOne = moment().subtract(1, "month").startOf("month").unix(); var endDateMonthMinusOne = moment().subtract(1, "month").endOf("month").unix(); // Previous month - 1 var startDateMonthMinusOne = moment().subtract(2, "month").startOf("month").unix(); var endDateMonthMinusOne = moment().subtract(2, "month").endOf("month").unix(); 

How can I do to get also the name of the month? (January February,...)

+7
javascript momentjs
source share
2 answers

Instead of unix() use the format() function to format the date-time using the MMMM format specifier for the month name.

 var monthMinusOneName = moment().subtract(1, "month").startOf("month").format('MMMM'); 

See the Display / Format chapter in the documentation.

+9
source share

You can simply use format('MMMM') .

Here is a working example:

 var currMonthName = moment().format('MMMM'); var prevMonthName = moment().subtract(1, "month").format('MMMM'); console.log(currMonthName); console.log(prevMonthName); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script> 
+3
source share

All Articles