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
- Installation
- Example in reactjs
Installation
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
- import the moment package
- example of show yesterday date with format
yyyy-mm-DD
let’s write the code.
import moment from "moment";
function App() {
return (
<div>
{/* example of show yesterday date with format `yyyy-mm-DD` */}
{moment().add(-1, 'days').format("yyyy-mm-DD")}
</div>
);
}
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.
I hope it’s help you, All the best đź‘Ť.