Navigate back to the homepage

how to subtract two dates in moment js?

Infinitbility
MomentJS
June 14th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To subtract two dates in moment js, just use the moment() and diff() method to subtract dates and get the difference in milliseconds ( You can specify your measurement unit like seconds, minutes, hours, days months, or years ).

Just import a moment in your file and use moment() to create a moment date after you can use the diff() method like the following example.

1var a = moment([2007, 0, 29]);
2var b = moment([2007, 0, 28]);
3a.diff(b) // 86400000

Today, I’m going to show How do I subtract two dates in moment js, here I will use the momentjs common method moment() to create the moment date object and the diff() method to get the difference.

Let’s start the today’s tutorial How do you subtract two dates in 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 24-hour format time HH:mm:ss
  3. example of 12-hour format time h:mm:ss a

let’s write the code.

1import moment from "moment";
2
3function subtractDates() {
4 var date1 = moment('2016-10-08 10:29:23');
5 var date2 = moment('2021-10-08 11:06:55');
6
7 // milliseconds
8 date2.diff(date1); // 157768652000
9
10 // seconds
11 console.log(date2.diff(date1, 'seconds')) // 157768652
12
13 // minutes
14 console.log(date2.diff(date1, 'minutes')) // 2629477
15
16 // hours
17 console.log(date2.diff(date1, 'hours')) // 43824
18
19 // days
20 console.log(date2.diff(date1, 'days')) // 1826
21
22 // weeks
23 console.log(date2.diff(date1, 'weeks')) // 260
24
25 // months
26 console.log(date2.diff(date1, 'months')) // 60
27
28 // years
29 console.log(date2.diff(date1, 'years')) // 5
30}
31
32function App() {
33 subtractDates()
34 return (
35 <div>
36 {/* example of 24 hour format time `HH:mm:ss` */}
37 {moment().format("HH:mm:ss")}
38 </div>
39 );
40}
41
42export default App;

Note: By default, moment#diff will truncate the result to zero decimal places, returning an integer. If you want a floating point number, pass true as the third argument.

1var a = moment([2008, 9]);
2var b = moment([2007, 0]);
3a.diff(b, 'years'); // 1
4a.diff(b, 'years', true); // 1.75

In the above program, we call the moment() and diff() methods to subtract dates and get differences in years, months, weeks, days, hours, minutes, and seconds.

let’s check the output.

MomentJs, Subtract dates example
MomentJs, Subtract dates example

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 convert string to date in moment js?

Example of convert string to date in moment js with step by step guide.

June 13th, 2022 · 1 min read

how to format time in moment js?

Example of format time in moment js with step by step guide.

June 13th, 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]