0

I want to compare two dates as which is bigger in those dates.

var date1 = 2011-9-2;
var date1 = 2011-17-06;

Can anybody say how can I do this?

Munerz
  • 1,052
  • 1
  • 13
  • 26
Sateesh
  • 11
  • 1
  • 2
  • 1
    How can this possibly be tagged "Java"? – adarshr Sep 02 '11 at 10:39
  • Related: http://stackoverflow.com/questions/7281937/check-the-dates-in-javascript – Rob Hruska Sep 02 '11 at 12:30
  • If you want to compare two date in javascript this post may help [Stakoverflow old thread](http://stackoverflow.com/questions/2752532/javascript-date-comparison) – nidhin Sep 02 '11 at 10:42
  • @Sateesh You might want to edit your question. `var date1 = 2011-9-2;` means compute 2011 minue 9 minus 2 and initialize the new variable `date1` to this value, which is 2000. Also accept one of the answers below. This question is over a week old. – Ray Toal Sep 12 '11 at 09:31

3 Answers3

2

You'll need to convert both strings to date objects first.

var date1 = new Date('2011-09-02');//yyyy-mm-dd format
var date2 = new Date('2011-06-17');
if(date1 > date2){
  alert('date1 is bigger than date2'); 
}

Once you have the 2 variables as date objects you can compare them against each other (without needing to convert to milliseconds/minutes/?)

scunliffe
  • 62,582
  • 25
  • 126
  • 161
1

Check this link

And then do something like this:

var days = 0;
var difference = 0;
Christmas = new Date("December 25, 2005");
today = new Date();
difference = Christmas - today;
days = Math.round(difference/(1000*60*60*24));

Code source

rlc
  • 5,809
  • 5
  • 38
  • 46
0

Create Date objects from your two values (check this link) and use that to do the comparison.

Bas Slagter
  • 9,831
  • 7
  • 47
  • 78