How can I check in JavaScript if two dates selected by the user are the same? The dates will be sent to this function as strings ("xx/xx/xxxx"). This level of detail is sufficient for my needs.
Below is the code I have written:
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 alerts showing the converted dates look 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 is functioning correctly. However, using the == or === operators does not change the 'valid' variable to false as expected.