Navigate back to the homepage

how to get previous month in moment js?

Infinitbility
MomentJS
June 17th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

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.

1moment().subtract(1, "month").format('MMMM')

Today, I’m going to show How do I get the previous month using moment js, here I will use the momentjs standard method subtract() and format() to get last month.

Let’s start today’s tutorial How do you get the previous month using moment js?

Table of content

  1. Installation
  2. Example in reactjs

Installation

Use the below installation command as per your package manager, moment support npm, Yarn, NuGet, spm, and meteor.

1npm install moment --save # npm
2yarn add moment # Yarn
3Install-Package Moment.js # NuGet
4spm install moment --save # spm
5meteor add momentjs:moment # meteor

Example in reactjs

In the following example, we are going to do

  1. import the moment package
  2. example of current date time to get last month
  3. example of custom date time to get last month

let’s write the code.

1import moment from "moment";
2
3function App() {
4 return (
5 <div>
6 {/* example of current date time to get last month */}
7 {console.log(moment().subtract(1, "month").format('MMMM'))}
8 {console.log("----")}
9 {/* example of custom date time to get last month */}
10 {console.log(moment('2016-10-08 10:29:23').subtract(1, "month").format('MMMM'))}
11 </div>
12 );
13}
14
15export default App;

In the above program, we call subtract() and format() to get a previous month of the current date or custom date.

let’s check the output.

MomentJs, Example of get last month of the date
MomentJs, Example of get last month of the date

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to get first and last date of month in moment js?

Example of get first and last date of month using moment js with step by step guide.

June 16th, 2022 · 1 min read

how to find the month name from the moment js date?

Example of get month from date using moment js with step by step guide.

June 16th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]