Determining Date Equality in Javascript
Asked Answered
H

5

99

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.

Hydrogenous answered 3/1, 2011 at 18:16 Comment(3)
Have you checked this post out: #338963. Does it help?Dannielledannon
Tempted to flag this as a duplicate but I think this is a fringe case of the same issue, so I'm not.Vevine
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.Ranket
C
180

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())
Crossquestion answered 3/1, 2011 at 18:20 Comment(2)
And the stackoverflow question I posted above shows an example. The questions were identical.Dannielledannon
A simpler way: if( +d1 == +d2 )Wilton
W
25

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

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

Witkin answered 11/9, 2013 at 13:56 Comment(7)
WTF? Why does that work but the equality operator does not? Where is the sense in that?Michal
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.Daryldaryle
Is this not a JS bug or is it a limitation of the equality operator and overloading ability?Blessed
This is what equality does for objects, it checks references.Witkin
@Witkin except for string, that are compared by value also, var a ="str1"; var b = "str"+"1"; a == b // trueUnsteel
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!Reply
@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/…Acidic
O
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)
Orlan answered 21/3, 2017 at 19:57 Comment(0)
J
0

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

if (d1 - d2 === 0)
Jelene answered 8/8, 2023 at 16:8 Comment(0)
C
-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

Chronometry answered 8/2, 2021 at 3:36 Comment(2)
The community encourages adding explanations alongisde code, rather than purely code-based answers (see here).Glynn
This is wrong. The 7th April is not the same as the 7th March.Creon

© 2022 - 2025 — McMap. All rights reserved.