This is a string comparison that you are doing, not a date comparison
In terms of strings ... 2 is greater than 0 ... That's why you always make the error "something is wrong"
EDIT: This is an explanation of what went wrong // Creates a date variable var D1 = new date ();
// Creates another date variable var D2 = new Date(); // Converts this date variable into a string D1 = 03-05-2014 // This is too is converted into a string D2 = 28-04-2014 00:00 dat = D2.split(' '); D2 = dat[0]; //finally D2 is 28-04-2014 <-- true, but it is a string if(D2<=D1){ // <-- At this point you are doing a string comparison echo "ok"; } else { echo "something is wrong"; }
EDIT: This is a possible solution.,
Do it instead
var D1 = new Date(); var D2 = new Date(); if(D2.getTime() <= D1.getTime()){ echo "ok"; } else { echo "something is wrong"; }
EDIT: If you get it from the input field do this
// Make sure you dates are in the format YYYY-MM-DD. // In other words, 28-04-2014 00:00 becomes 2014-04-28 00:00 // Javascript Date only accepts the ISO format by default var D1 = new Date( $('#input1#').val() ); var D2 = new Date( $('#input2#').val() ); if(D2.getTime() <= D1.getTime()){ echo "ok"; } else { echo "something is wrong"; }
Abimbola esuruoso
source share