Categories
compare date datetime javascript

Compare two dates with JavaScript

2553

Can someone suggest a way to compare the values of two dates greater than, less than, and not in the past using JavaScript? The values will be coming from text boxes.

7

  • 18

    When it comes to DateTime and manipulation in JS, I look no further than momentjs 🙂

    – Halceyon

    Sep 23, 2013 at 13:35

  • 86

    no need to use momentjs to compare 2 dates. Just use pure javascript’s Date object. Check main answer for more details.

    Jun 9, 2016 at 5:26

  • You can refer following answer : stackoverflow.com/questions/4812152/… Check getDateDifference and getDifferenceInDays if it can help.

    Nov 3, 2016 at 10:46


  • I’ll give you a reason to look further than Moment.js (which I love, BUT…): Don’t use for looping loads

    May 29, 2017 at 20:07

  • 4

    For those like me who may have come along later, moment.js is now in “maintenance mode,” i.e. no longer being actively developed.

    – Bob Brown

    May 2, 2021 at 15:03

3051

The Date object will do what you want – construct one for each date, then compare them using the >, <, <= or >=.

The ==, !=, ===, and !== operators require you to use date.getTime() as in

var d1 = new Date();
var d2 = new Date(d1);
var same = d1.getTime() === d2.getTime();
var notSame = d1.getTime() !== d2.getTime();

to be clear just checking for equality directly with the date objects won’t work

var d1 = new Date();
var d2 = new Date(d1);

console.log(d1 == d2);   // prints false (wrong!) 
console.log(d1 === d2);  // prints false (wrong!)
console.log(d1 != d2);   // prints true  (wrong!)
console.log(d1 !== d2);  // prints true  (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)

I suggest you use drop-downs or some similar constrained form of date entry rather than text boxes, though, lest you find yourself in input validation hell.


For the curious, date.getTime() documentation:

Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC. (Negative values are returned for prior times.)

4

  • 78

    Even Ace’s method is not failsafe. You need to reset the milliseconds first, and you might even want to reset the whole time. date1.setHours(0); date1.setMinutes(0); date1.setSeconds(0); date1.setMilliseconds(0); That combined with using .getTime() will give you an accurate compare result

    – patrick

    Dec 13, 2011 at 2:23

  • 128

    @patrick, suggest calling setHours(0,0,0,0) this way. Eliminates the need for calling setMinutes() etc. Also, executes faster.

    – Karl

    Nov 16, 2012 at 21:32

  • 29

    avoiding == or === to get desired result: jsfiddle.net/P4y5J now >= anotherNow && now <= anotherNow IS true FYI

    Apr 15, 2014 at 19:42

  • 17

    You can also compare the numeric values of your dates in order to avoid comparing the objects themselves: date1.valueOf() == date2.valueOf()

    – madprog

    Nov 6, 2014 at 13:56

486

The easiest way to compare dates in javascript is to first convert it to a Date object and then compare these date-objects.

Below you find an object with three functions:

  • dates.compare(a,b)

    Returns a number:

    • -1 if a < b
    • 0 if a = b
    • 1 if a > b
    • NaN if a or b is an illegal date
  • dates.inRange (d,start,end)

    Returns a boolean or NaN:

    • true if d is between the start and end (inclusive)
    • false if d is before start or after end.
    • NaN if one or more of the dates are illegal.
  • dates.convert

    Used by the other functions to convert their input to a date object. The input can be

    • a date-object : The input is returned as is.
    • an array: Interpreted as [year,month,day]. NOTE month is 0-11.
    • a number : Interpreted as number of milliseconds since 1 Jan 1970 (a timestamp)
    • a string : Several different formats is supported, like “YYYY/MM/DD”, “MM/DD/YYYY”, “Jan 31 2009” etc.
    • an object: Interpreted as an object with year, month and date attributes. NOTE month is 0-11.

.

// Source: http://stackoverflow.com/questions/497790
var dates = {
    convert:function(d) {
        // Converts the date in d to a date-object. The input can be:
        //   a date object: returned without modification
        //  an array      : Interpreted as [year,month,day]. NOTE: month is 0-11.
        //   a number     : Interpreted as number of milliseconds
        //                  since 1 Jan 1970 (a timestamp) 
        //   a string     : Any format supported by the javascript engine, like
        //                  "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
        //  an object     : Interpreted as an object with year, month and date
        //                  attributes.  **NOTE** month is 0-11.
        return (
            d.constructor === Date ? d :
            d.constructor === Array ? new Date(d[0],d[1],d[2]) :
            d.constructor === Number ? new Date(d) :
            d.constructor === String ? new Date(d) :
            typeof d === "object" ? new Date(d.year,d.month,d.date) :
            NaN
        );
    },
    compare:function(a,b) {
        // Compare two dates (could be of any type supported by the convert
        // function above) and returns:
        //  -1 : if a < b
        //   0 : if a = b
        //   1 : if a > b
        // NaN : if a or b is an illegal date
        // NOTE: The code inside isFinite does an assignment (=).
        return (
            isFinite(a=this.convert(a).valueOf()) &&
            isFinite(b=this.convert(b).valueOf()) ?
            (a>b)-(a<b) :
            NaN
        );
    },
    inRange:function(d,start,end) {
        // Checks if date in d is between dates in start and end.
        // Returns a boolean or NaN:
        //    true  : if d is between start and end (inclusive)
        //    false : if d is before start or after end
        //    NaN   : if one or more of the dates is illegal.
        // NOTE: The code inside isFinite does an assignment (=).
       return (
            isFinite(d=this.convert(d).valueOf()) &&
            isFinite(start=this.convert(start).valueOf()) &&
            isFinite(end=this.convert(end).valueOf()) ?
            start <= d && d <= end :
            NaN
        );
    }
}

1

  • 16

    (a > b) - (a < b) is useful for sorting dates array

    – nktssh

    May 14, 2015 at 9:26

464

Compare < and > just as usual, but anything involving == or === should use a + prefix. Like so:

const x = new Date('2013-05-23');
const y = new Date('2013-05-23');

// less than, greater than is fine:
console.log('x < y', x < y); // false
console.log('x > y', x > y); // false
console.log('x <= y', x <= y); // true
console.log('x >= y', x >= y); // true
console.log('x === y', x === y); // false, oops!

// anything involving '==' or '===' should use the '+' prefix
// it will then compare the dates' millisecond values

console.log('+x === +y', +x === +y); // true

4

  • 126

    terribly slow 🙂 I prefer x.getTime() === y.getTime() method, both readable and extremely fast see jsperf

    Dec 12, 2013 at 9:56

  • 36

    The + operator attempts to convert the expression into a Number. Date.valueOf() is used for the conversion (which returns the same thing as Date.getTime().

    – Salman A

    Jan 20, 2014 at 9:30

  • @daniel < and <= use same algorithm (abstract relational comparison), likewise for > and >=. The answer was popular but wrong. Please correct the first line which I forgot to edit.

    – Salman A

    Oct 29, 2021 at 10:13

  • 1

    @huysentruitw The original jsperf URL is down, but it has been archived on web.archive.org. You can even run the benchmark from the archived version. TL;DR: comparing using .getTime() is at least 100 times quicker than comparing the Date objects directly.

    Jun 24 at 0:44