views:

137

answers:

2

I need to create a javascript function that checks if it has been a day since timeX (an instance of Date). I do NOT mean whether is has been 24 hours since timeX, but instead whether it has passed a midnight since timeX.

I am a PHP expert, not a JavaScript one, so I was wondering if anyone here had any quick answers. Thanks!

function(dateLast, dateNow) {...}
+1  A: 
if(dateNow.getDate() == dateLast.getDate()){
 //it's the same day
}else{
 //another day, so midnight has passed
}

EDIT: changed the condition, now it should work.

oezi
Could be an error if the dates are exactly 1 month apart. Almost though
Jonah
What happens if the two dates are 2010-04-15 and 2010-05-15?
Thomas
+2  A: 

A simple solution to check whether two dates represent the same day is:

function isSameDay(a, b) {
    return a.toDateString() == b.toDateString();
}

This works no matter what the distance between the two dates is.

Max Shawabkeh
Excellent! Thanks!
Jonah