Skip to content Skip to sidebar Skip to footer

How To Compare Date In Javascript

I want to compare following to dates i.e. d1 with d2: var d1 = new Date(12,05,2013); var d2 = '12/05/2013';

Solution 1:

convert date to timestamp

DateObject.getTime(); will give timestamp

and convert string to date new Date(d2)

javScript

var d1 = newDate("12/05/2013");
var d2 = "12/05/2013";
console.log(d1.getTime());
console.log(newDate(d2).getTime());

if(d1.getTime() == newDate(d2).getTime()){
   //do something
}

Solution 2:

To compare two variables you need to use JS operators, consider:

if(d1==d2)
{
 //do this
}

else
{
 //do this
}

More information on logical operators can be found here: http://www.w3schools.com/js/js_comparisons.asp

Post a Comment for "How To Compare Date In Javascript"