What is wrong with the code below?
Maybe it would be simpler to just compare date and not time. I am not sure how to do this either, and I searched, but I couldn\'t
As I don't see here similar approach, and I'm not enjoying setting h/m/s/ms to 0, as it can cause problems with accurate transition to local time zone with changed date
object (I presume so), let me introduce here this, written few moments ago, lil function:
+
: Easy to use, makes a basic comparison operations done (comparing day, month and year without time.)
-
: It seems that this is a complete opposite of "out of the box" thinking.
function datecompare(date1, sign, date2) {
var day1 = date1.getDate();
var mon1 = date1.getMonth();
var year1 = date1.getFullYear();
var day2 = date2.getDate();
var mon2 = date2.getMonth();
var year2 = date2.getFullYear();
if (sign === '===') {
if (day1 === day2 && mon1 === mon2 && year1 === year2) return true;
else return false;
}
else if (sign === '>') {
if (year1 > year2) return true;
else if (year1 === year2 && mon1 > mon2) return true;
else if (year1 === year2 && mon1 === mon2 && day1 > day2) return true;
else return false;
}
}
Usage:
datecompare(date1, '===', date2)
for equality check,
datecompare(date1, '>', date2)
for greater check,
!datecompare(date1, '>', date2)
for less or equal check
Also, obviously, you can switch date1
and date2
in places to achieve any other simple comparison.