Navigate back to the homepage

how to get number of days between two dates in moment js?

Infinitbility
MomentJS
June 17th, 2022 · 1 min read

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.

1a.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

  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 getting difference of two dates in a days

let’s write the code.

1import moment from "moment";
2
3function consoleTheDiff() {
4 let start = moment("2018-03-10");
5 let end = moment("2018-03-15");
6 let diff = end.diff(start, 'days')
7 console.log("Number of days", diff);
8}
9
10function App() {
11 consoleTheDiff()
12 return (
13 <div>Hi 👋, ❤️ From Infinitbility</div>
14 );
15}
16
17export 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.

MomentJs, Example of get number of days between two dates
MomentJs, Example of get number of days between two dates

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 previous month in moment js?

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

June 17th, 2022 · 1 min read

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
© 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]