Navigate back to the homepage

how to compare two dates in moment js?

Infinitbility
MomentJS
June 11th, 2022 · 2 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To compare two dates in moment js, The momentjs provide isBefore(), isSame(), isAfter(), isSameOrBefore(), isSameOrAfter(), and isBetween() to compare dates as you want.

Today, I’m going to show How do I compare two dates in moment js, here I will use the momentjs above-mentioned functions with example to compare dates.

Let’s start the today’s tutorial How do you compare two dates in moment js?

Table of content

  1. Installation
  2. Check if a date is before another date
  3. Check if a date is the same as another date
  4. Check if a date is after another date
  5. Check if a date is the same or before another date
  6. Check if a date is the same or after another date
  7. Check if a date is between other dates
  8. Examples 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

Check if a date is before another date

The momentjs provide isBefore() method to check date is before another date or not if its return is true means the date is before another date.

1moment('2010-10-20').isBefore('2010-10-21'); // true

You can specify what you are checking like year, month, week, isoWeek, day, hour, minute, and second using isBefore() second parameter like below example.

1moment('2010-10-20').isBefore('2010-12-31', 'year'); // false
2moment('2010-10-20').isBefore('2011-01-01', 'year'); // true

Check if a date is same another date

The momentjs provide the isSame() method to check date is the same as another date or not, if it returns true means the date is the same as another date.

1moment('2010-10-20').isSame('2010-10-20'); // true

You can specify what you are checking like year, month, week, isoWeek, day, hour, minute, and second using isSame() second parameter like below example.

1moment('2010-10-20').isSame('2009-12-31', 'year'); // false
2moment('2010-10-20').isSame('2010-01-01', 'year'); // true
3moment('2010-10-20').isSame('2010-12-31', 'year'); // true
4moment('2010-10-20').isSame('2011-01-01', 'year'); // false

When including a second parameter, it will match all units equal or larger. Passing in month will check month and year. Passing in day will check day, month, and year.

1moment('2010-01-01').isSame('2011-01-01', 'month'); // false, different year
2moment('2010-01-01').isSame('2010-02-01', 'day'); // false, different month

Check if a date is after another date

The momentjs provide the isAfter() method to check date is after another date or not, if its return is true means the date is after another date.

1moment('2010-10-20').isAfter('2010-10-19'); // true

You can specify what you are checking like year, month, week, isoWeek, day, hour, minute, and second using isAfter() second parameter like below example.

1moment('2010-10-20').isAfter('2010-01-01', 'year'); // false
2moment('2010-10-20').isAfter('2009-12-31', 'year'); // true

Check if a date is same or before another date

The momentjs provide the isSameOrBefore() method to check date is the same or before another date or not if its return true means the date is the same or before another date.

1moment('2010-10-20').isSameOrBefore('2010-10-21'); // true
2moment('2010-10-20').isSameOrBefore('2010-10-20'); // true
3moment('2010-10-20').isSameOrBefore('2010-10-19'); // false

You can specify what you are checking like year, month, week, isoWeek, day, hour, minute, and second using isSameOrBefore() second parameter like below example.

1moment('2010-10-20').isSameOrBefore('2009-12-31', 'year'); // false
2moment('2010-10-20').isSameOrBefore('2010-12-31', 'year'); // true
3moment('2010-10-20').isSameOrBefore('2011-01-01', 'year'); // true

Check if a date is same or after another date

The momentjs provide the isSameOrAfter() method to check date is the same or after another date or not if it returns true means the date is the same or after another date.

1moment('2010-10-20').isSameOrAfter('2010-10-19'); // true
2moment('2010-10-20').isSameOrAfter('2010-10-20'); // true
3moment('2010-10-20').isSameOrAfter('2010-10-21'); // false

You can specify what you are checking like year, month, week, isoWeek, day, hour, minute, and second using isSameOrAfter() second parameter like below example.

1moment('2010-10-20').isSameOrAfter('2011-12-31', 'year'); // false
2moment('2010-10-20').isSameOrAfter('2010-01-01', 'year'); // true
3moment('2010-10-20').isSameOrAfter('2009-12-31', 'year'); // true

Check if a date is between another dates

The momentjs provide the isBetween() method to check date is between another date or not if it returns true means the date is between another date.

1moment('2010-10-20').isBetween('2010-10-19', '2010-10-25'); // true
2moment('2010-10-20').isBetween('2010-10-25', '2010-10-19'); // false

You can specify what you are checking like year, month, week, isoWeek, day, hour, minute, and second using isBetween() third parameter like below example.

1moment('2010-10-20').isBetween('2010-01-01', '2012-01-01', 'year'); // false
2moment('2010-10-20').isBetween('2009-12-31', '2012-01-01', 'year'); // true

Example in reactjs

In the following example, we are going to do, Put all the above function examples in a function and check their output.

let’s write the code.

1import moment from "moment";
2
3function consoleTheMoment() {
4 console.log("------- Is Before -------");
5 console.log("moment('2010-10-20').isBefore('2010-10-21')", moment('2010-10-20').isBefore('2010-10-21'))
6
7 console.log("------- Is Same -------");
8 console.log("moment('2010-10-20').isSame('2010-10-20')", moment('2010-10-20').isSame('2010-10-20'))
9
10 console.log("------- Is After -------");
11 console.log("moment('2010-10-20').isAfter('2010-10-19')", moment('2010-10-20').isAfter('2010-10-19'))
12
13 console.log("------- Is same or before -------");
14 console.log("moment('2010-10-20').isSameOrBefore('2010-10-21')", moment('2010-10-20').isSameOrBefore('2010-10-21'))
15 console.log("moment('2010-10-20').isSameOrBefore('2010-10-20')", moment('2010-10-20').isSameOrBefore('2010-10-20'))
16 console.log("moment('2010-10-20').isSameOrBefore('2010-10-19')", moment('2010-10-20').isSameOrBefore('2010-10-19'))
17
18 console.log("------- Is same or after -------");
19 console.log("moment('2010-10-20').isSameOrAfter('2010-10-19')", moment('2010-10-20').isSameOrAfter('2010-10-19'))
20 console.log("moment('2010-10-20').isSameOrAfter('2010-10-20')", moment('2010-10-20').isSameOrAfter('2010-10-20'))
21 console.log("moment('2010-10-20').isSameOrAfter('2010-10-21')", moment('2010-10-20').isSameOrAfter('2010-10-21'))
22
23 console.log("------- Is between -------");
24 console.log("moment('2010-10-20').isBetween('2010-10-19', '2010-10-25')", moment('2010-10-20').isBetween('2010-10-19', '2010-10-25'))
25 console.log("moment('2010-10-20').isBetween('2010-10-25', '2010-10-19')", moment('2010-10-20').isBetween('2010-10-25', '2010-10-19'))
26}
27
28function App() {
29 consoleTheMoment()
30 return (
31 <div>
32 {moment().format("hh:mm:ss")}
33 </div>
34 );
35}
36
37export default App;

In the above program, we call moment comparison methods in simple function to compare dates. let’s check the output.

MomentJS, compare dates example
MomentJS, compare 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 get difference between two dates in weeks in javascript?

Example of get weeks between two dates in javascript with step by step guide.

June 10th, 2022 · 1 min read

how to get current date in moment js?

Example of get current date in moment js with step by step guide.

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