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, ...)
This article goes in detailed on jquery moment get month from date. Let's see bellow example how to get month name from date in moment js. Here, i will give you simple example of jquery moment js get month name from date using format('MMMM').
To get the previous month in moment js, use the subtract(1, "month") method to subtract the month in date and use format('MMMM') to get the month name from subtracted date. Just import moment in your file and call moment(). subtract(1, "month"). format('MMMM') and it will return previous month.
The moment(). hour() Method is used to get the hours from the current time or to set the hours. Syntax: moment().hour(); or. moment().
subtract(1, 'months'); but this does: moment("2017-12-01"). subtract(1, 'months'). format('MMMM DD h:mm A'); why the format is necessary?
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With