Hi Friends 👋,
Welcome To Infinitbility! ❤️
To get the number of days between two dates in moment js, use the diff()
method with days
to get the difference in a day.
Just import a moment in your file and call a.diff(b, 'days')
and it will return the difference of dates in a day.
a.diff(b, 'days')
Today, I’m going to show How do I get a number of days between two dates using moment js, here I will use the momentjs standard method moment()
and diff()
for a number of days between two dates.
Let’s start today’s tutorial How do you get the number of days between two dates using 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 getting difference of two dates in a days
let’s write the code.
import moment from "moment";
function consoleTheDiff() {
let start = moment("2018-03-10");
let end = moment("2018-03-15");
let diff = end.diff(start, 'days')
console.log("Number of days", diff);
}
function App() {
consoleTheDiff()
return (
<div>Hi 👋, ❤️ From Infinitbility</div>
);
}
export default App;
In the above program, we have created a two-moment dates variable and used the moment diff()
method with the days
parameter to get the difference in days.
let’s check the output.
I hope it’s help you, All the best 👍.