Categories
JavaScript Answers

How to get month name of last month using moment and JavaScript?

Spread the love

Sometimes, we want to get month name of last month using moment and JavaScript.

In this article, we’ll look at how to get month name of last month using moment and JavaScript.

How to get month name of last month using moment and JavaScript?

To get month name of last month using moment and JavaScript, we use the format method.

For instance, we write

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

to call subtract to subtract 1 month from today’s date.

We call startOf to return the start date of the month.

And we call format with 'MMMM' to return the month name.

Conclusion

To get month name of last month using moment and JavaScript, we use the format method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *