[Solved-6 Solutions] Compare two dates with JavaScript - javascript tutorial
Problem:
How to compare two dates with JavaScript ?
Solution 1:
The following code defines how to compare two dates:
<script type="text/javascript" language="javascript">
function CompareDate() {
//Note: 00 is month i.e. January
var dateOne = new Date(2012, 00, 25); //Year, Month, Date
var dateTwo = new Date(2011, 00, 25); //Year, Month, Date
if (dateOne > dateTwo) {
alert("Date One is greather then Date Two.");
}
else
{
alert("Date Two is greather then Date One.");
}
}
CompareDate();
</script>
Solution 2:
The ==
, !=
, ===
, and !==
operators are required to use date.getTime()
var a = new Date();
var b = new Date(a);
var same = a.getTime() === b.getTime();
var notSame = a.getTime() !== b.getTime();
Checking for equality directly with the data objects won't work
var a = new Date();
var b = new Date(a);
console.log(a == b); // prints false (wrong!)
console.log(a === b); // prints false (wrong!)
console.log(a != b); // prints true (wrong!)
console.log(a !== b); // prints true (wrong!)
console.log(a.getTime() === b.getTime()); // prints true (correct)
Solution 3:
The easiest way to compare dates in javascript is to first convert it to a Date object and then compare these date objects. Below three functions are used to find an object:
1. dates.compare(a,b)
- Returns a number:
- -1 if a
- 0 if a = b
- 1 if a > b
- NaN if a or b is an illegal date
2. 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.
3. 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.
- 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 01 2000" etc.
- An object : Interpreted as an object with year, month and date attributes.
Note : month is 0-11.
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
);
}
}
Solution 4:
Compare <
and >
as normal, but anything involving =
should use a +
prefix.
var a = new Date('2013-05-23');
var b = new Date('2013-05-23');
// less than, greater than is fine:
a < b; => false
a > b; => false
a === b; => false, oops!
// anything involving '=' should use the '+' prefix
// it will then compare the dates' millisecond values
+a <= +b; => true
+a >= +b; => true
+a === +b; => true
Read Also
Javascript get current dateSolution 5:
<
<=
>
>=
these relational operators can be used to compare JavaScript dates:
var a = new Date(2013, 0, 1);
var b = new Date(2013, 0, 2);
a < b; // true
a <= b; // true
a > b; // false
a >= b; // false
However, the equality operators ==
!=
===
!==
cannot be used to compare the dates because:
- Two distinct objects are never equal for either strict or abstract comparisons.
- An expression comparing Objects is only true if the operands reference the same Object.
We can compare the value of dates for equality using any of these methods:
var a = new Date(2013, 0, 1);
var b = new Date(2013, 0, 1);
/*
* note: a == b returns false as described above
*/
a.getTime() == b.getTime(); // true
a.valueOf() == b.valueOf(); // true
Number(a) == Number(b); // true
+a == +b; // true
Solution 6:
This method is used to subtract from one date to other date.
var oDateOne = new Date();
var DateTwo = new Date();
alert(DateOne - DateTwo === 0);
alert(DateOne - DateTwo < 0);
alert(DateOne - DateTwo > 0);