98

I need to find out if two dates the user selects are the same in Javascript. The dates are passed to this function in a String ("xx/xx/xxxx").That is all the granularity I need.

Here is my code:

        var valid = true;
    var d1 = new Date($('#datein').val());
    var d2 = new Date($('#dateout').val());
    alert(d1+"\n"+d2);
    if(d1 > d2) {
        alert("Your check out date must be after your check in date.");
        valid = false;
    } else if(d1 == d2) {
        alert("You cannot check out on the same day you check in.");
        valid = false;
    }

The javascript alert after converting the dates to objects looks like this:

Tue Jan 25 2011 00:00:00 GMT-0800 (Pacific Standard Time)

Tue Jan 25 2011 00:00:00 GMT-0800 (Pacific Standard Time)

The test to determine if date 1 is greater than date 2 works. But using the == or === operators do not change valid to false.

Jarred
  • 1,986
  • 5
  • 27
  • 42
  • 3
    Have you checked this post out: http://stackoverflow.com/questions/338463/how-do-i-do-a-date-comparison-in-javascript. Does it help? – JohnMerlino Jan 03 '11 at 18:21
  • Tempted to flag this as a duplicate but I think this is a fringe case of the same issue, so I'm not. – Liam Nov 20 '13 at 10:52
  • This is not a duplicate. The post mentionned (and the accepted answer) are about determining if a date is before or after another, not about equality. – yannick1976 Jul 23 '15 at 17:49

5 Answers5

178

Use the getTime() method. It will check the numeric value of the date and it will work for both the greater than/less than checks as well as the equals checks.

EDIT:

if (d1.getTime() === d2.getTime())
Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81
spinon
  • 10,760
  • 5
  • 41
  • 59
24

If you don't want to call getTime() just try this:

(a >= b && a <= b)

devmiles.com
  • 9,895
  • 5
  • 31
  • 47
  • 26
    WTF? Why does that work but the equality operator does not? Where is the sense in that? – Kuepper Sep 20 '13 at 12:56
  • 12
    The equality operator checks for reference equality. This means it only returns true if the two variables refer the the same object. If you create two Date objects (`var a = new Date(); var b = new Date();`), they will never be equal. – Tomas Oct 11 '13 at 09:40
  • Is this not a JS bug or is it a limitation of the equality operator and overloading ability? – Josh M. Oct 29 '13 at 20:41
  • 2
    This is what equality does for objects, it checks references. – devmiles.com Oct 30 '13 at 11:00
  • 5
    @devmiles.com except for string, that are compared by value also, `var a ="str1"; var b = "str"+"1"; a == b // true` – amd Jun 18 '14 at 11:57
  • 11
    wierd. You would have thought this would have actually been a place where a seperation between == and === would make sense... Oh wait its javascript. We dont do sense here! – JonnyRaa Aug 13 '14 at 11:13
  • 10
    @Towa, That works because the >= and <= operators only work on numbers, so they try to convert their operands to numbers. And to convert an object (such as a Date) to a number, JavaScript calls the valueOf() function to get a numeric representation. valueOf() returns the same thing as getTime(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf – Mike Schenk Dec 09 '16 at 21:34
3
var d1 = new Date($('#datein').val());
var d2 = new Date($('#dateout').val());

use two simple ways to check equality

  1. if( d1.toString() === d2.toString())
  2. if( +d1 === +d2)
0

Instead of comparing the dates directly, you can check that their difference is zero.

if (d1 - d2 === 0)
Liam Bohl
  • 375
  • 1
  • 2
  • 12
-3
var date = Wed Oct 07 2015 19:48:08 GMT+0200 (Central European Daylight Time);

var dateOne = new Date(date);
var dateTwo = new Date();

var isEqual = dateOne.getDate() === dateTwo.getDate()

this will give you the dates equality

Jack Van
  • 125
  • 1
  • 1
  • 4
  • The community encourages adding explanations alongisde code, rather than purely code-based answers (see [here](https://meta.stackoverflow.com/questions/300837/what-comment-should-i-add-to-code-only-answers)). – costaparas Feb 08 '21 at 05:30
  • 3
    This is wrong. The 7th April is not the same as the 7th March. – Mark Fisher Apr 07 '21 at 20:45