-5

Possible Duplicate:
Compare dates with JavaScript

I want to find which one is greater from 2 dates with time that is in the following format in javascript

date1=2-11-2012 13:40:00 date2=01-11-2012 10:40:00

Community
  • 1
  • 1
user1490612
  • 289
  • 2
  • 4
  • 8

2 Answers2

11

Hopefully you can learn something...

Then, you may come up with something like...

var isLarger = new Date("2-11-2012 13:40:00") > new Date("01-11-2012 10:40:00");
alex
  • 479,566
  • 201
  • 878
  • 984
  • 1
    correct answer, but is the `+` before those dates necessary? i think you can compare Date objects directly. – Dennis Nov 02 '12 at 07:17
  • @Chips_100 I was confused for when you need them. The comparison operator seems to call their `valueOf()` implicitly. – alex Nov 02 '12 at 07:21
  • new Date("2-11-2012 13:40:00") will give an invalid date object and +new Date("2-11-2012 13:40:00") will give NaN. How they can be compared? – Ravindra Gullapalli Nov 02 '12 at 07:21
  • @RavindraGullapalli [It doesn't give me an invalid date](http://cl.ly/image/3o2f1a0P1v12). – alex Nov 02 '12 at 07:22
  • @alex [Check This](http://twitpic.com/b9k7p7) – Ravindra Gullapalli Nov 02 '12 at 07:35
  • new Date("2-11-2012 13:40:00") > new Date("01-12-2012 10:40:00"); this is also giving "true" only. so its not working – albert Jegani Feb 19 '15 at 10:58
  • @albertJegani If you look at the dates in those objects, you can see that the first one is 11th Feb 2012 and the second is 12th Jan 2012, so the comparison *is* correct. Your expectations at how the string would be parsed is incorrect. – alex Feb 19 '15 at 22:27
  • yes alex, its correct – albert Jegani Feb 20 '15 at 09:41
1
var x=new Date();
x.setFullYear(2100,0,14);
var y=new Date();
y.setFullYear(2100,0,20);

if (x>y)
{
 alert("Message1");
}
else
{
alert("Message2");
}
Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
vikbehal
  • 1,486
  • 3
  • 21
  • 48