-4

I want to compare 2 dates in the following format shown below in variables a and b . I want to check if a is greater than b but the code below isnt working since its not a regular number

var a = "4/29/2015";
var b = "4/10/2015";

if(a > b){
alert("working");
}
Matt
  • 173
  • 1
  • 13

2 Answers2

3

Convert the strings to Date objects.

var a = new Date("4/29/2015");
var b = new Date("4/10/2015");

if (a > b) {
  console.log(a + " is greater than " + b);
}
forgivenson
  • 4,394
  • 2
  • 19
  • 28
0

Use javascript's Date object:

//2nd Argument month goes from 0(January) to 11(December)
var a = new Date(2015, 3, 29);
var b = new Date(2015, 3, 10);

if(a > b){
  alert("working");
}
Curious
  • 2,783
  • 3
  • 29
  • 45
  • But the OP's input dates are in string format. How does he extract the parts of the date? – nnnnnn Apr 29 '15 at 12:14
  • Well, even date strings can be used to construct `Date` objects, I see there are other answers using the same... – Curious Apr 29 '15 at 12:16