105

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.

3
  • 4
    Have you checked this post out: stackoverflow.com/questions/338463/…. Does it help? Commented Jan 3, 2011 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. Commented Nov 20, 2013 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. Commented Jul 23, 2015 at 17:49

5 Answers 5

186

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())
Sign up to request clarification or add additional context in comments.

2 Comments

And the stackoverflow question I posted above shows an example. The questions were identical.
A simpler way: if( +d1 == +d2 )
26

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

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

7 Comments

WTF? Why does that work but the equality operator does not? Where is the sense in that?
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.
@devmiles.com except for string, that are compared by value also, var a ="str1"; var b = "str"+"1"; a == b // true
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!
@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(): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
|
4
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)

Comments

0

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

if (d1 - d2 === 0)

Comments

-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

2 Comments

The community encourages adding explanations alongisde code, rather than purely code-based answers (see here).
This is wrong. The 7th April is not the same as the 7th March.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.