50

I use the following code to get startDate and endDate of the last 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 month name ? (January, February, ...)

wawanopoulos
  • 9,614
  • 31
  • 111
  • 166

2 Answers2

71

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

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

See the chapter Display / Format in the documentation

NineBerry
  • 26,306
  • 3
  • 62
  • 93
58

You can simply use format('MMMM').

Here 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>
VincenzoC
  • 30,117
  • 12
  • 90
  • 112