Hi Friends 👋,

Welcome To Infinitbility! ❤️

To get yesterday date in moment js, pass -1 and days in moment().add() method it will subtract 1 day in current date and return yesterday date.

Just import a moment in your file and use moment().add(-1, 'days') to get yesterday date.

moment().add(-1, 'days')

Today, I’m going to show How do I get yesterday’s date in moment js, here I will use the momentjs common method moment().add() to show yesterday’s date.

Let’s start the today’s tutorial How do you get yesterday’s date in moment js?

Table of content

  1. Installation
  2. Example in reactjs


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

npm install moment --save   # npm
yarn add moment             # Yarn
Install-Package Moment.js   # NuGet
spm install moment --save   # spm
meteor add momentjs:moment  # meteor

Example in reactjs

In the following example, we are going to do

  1. import the moment package
  2. example of show yesterday date with format yyyy-mm-DD

let’s write the code.

import moment from "moment";

function App() {
  return (
        {/* example of show yesterday date with format `yyyy-mm-DD` */}
        {moment().add(-1, 'days').format("yyyy-mm-DD")}

export default App;

Note: By default, add() method is used for addition in date that’s why we are passing -1 to get yesterday’s date

In the above program, we call the moment().add(-1, 'days') and format("yyyy-mm-dd") methods to show formated yesterday date.

let’s check the output.

MomentJs, Yesterday date example
MomentJs, Yesterday date example

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