• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 966
  • Last Modified:

js / jQuery compare dates <=

I am displaying a specific div, based on a date comparison of "<=".  I can get the "<" to work fine. The issues is when the dates are "=" to each other, it skips this function when it should not. I want to compare just the date and not the seconds, that is the problem.

Date {Sat Sep 01 2012 12:13:57 GMT-0700 (Pacific Daylight Time)}
Like this "Sat Sep 01 2012"

Date {Thu Aug 09 2012 00:00:00 GMT-0700 (Pacific Daylight Time)}
Like this "Thu Aug 09 2012"

var now = new Date();
var showMe1 = new Date("August 09, 2012");
var showMe2 = new Date("August 17, 2012");

if (now <= showMe1){
$('#myScroll').goToPage(1);
return false;

}else if (now <= showMe2){
$('#myScroll').goToPage(2);
return false;
}

Open in new window

0
gvilla23
Asked:
gvilla23
  • 4
  • 4
  • 2
1 Solution
 
GwynforWebCommented:
Try this

<script>
function compareDate(d1,d2){
 // returns d1 <= d2

 if (d1.getYear() > d2.getYear()) return false;
 if (d1.getYear() < d2.getYear()) return true;

 if (d1.getMonth() > d2.getMonth()) return false;
 if (d1.getMonth() < d2.getMonth()) return true;

 if (d1.getDate() > d2.getDate()) return false;
 if (d1.getDate() < d2.getDate()) return true;

 return true
}

var now = new Date();
var showMe1 = new Date("August 09, 2012");
var showMe2 = new Date("August 17, 2012");

if (compareDate(now,showMe1)){
$('#myScroll').goToPage(1);
return false;

}else if (compareDate(now,showMe2)){
$('#myScroll').goToPage(2);
return false;
}
                                  
</script>

Open in new window

Due to the order of evaluations of operations this function also works

<script>
function compareDate(d1,d2){
 // returns d1 <= d2

 return  (d1.getYear() <= d2.getYear()) && (d1.getMonth <= d2.getMonth) && (d1.getDate <= d2.getDate)
}                               
</script>

Open in new window

0
 
tdlewisCommented:
The solution provided by @GwynforWeb should accomplish what you need, but I thought an explanation might also be helpful. The reason date1 = date2 is not working is because they have to match to the millisecond for the equality test to work.
0
 
gvilla23Author Commented:
Thank you, but I have a total of 20 dates to compare. Would this approach be efficient?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
tdlewisCommented:
The alternative to the solution posted by @GwynforWeb is to ensure that the time is always stripped from the variable before you make the comparison. You would modify your sample code as follows:
var now = new Date();
now.SetHours(0,0,0,0); // clears the time fields;
var showMe1 = new Date("August 09, 2012");
var showMe2 = new Date("August 17, 2012");

if (now <= showMe1){
$('#myScroll').goToPage(1);
return false;

}else if (now <= showMe2){
$('#myScroll').goToPage(2);
return false;
}

Open in new window

0
 
GwynforWebCommented:
The time taken by the functions I provided would be minimal even with 20 dates needing comparison. There is really no other faster way of doing it. The second function I provided would be faster but the code is less clear imo.

The only true way to find out is to test it. But frankly your task is not time intensive at all relative to the power of the JavaScript interpreter.
0
 
gvilla23Author Commented:
I get this error when I use this, now.SetHours(0,0,0,0); // clears the time fields;
TypeError: now.SetHours is not a function
0
 
GwynforWebCommented:
It is

now.setHours(0,0,0,0)
0
 
gvilla23Author Commented:
That was it! thank you very much.
0
 
gvilla23Author Commented:
Excellent!
0
 
GwynforWebCommented:
ty.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now