0

I am trying to compare two dates using javascript,The datetime format is given below.Here i want to check date1 > date2.How can i achive this in javascript.

 var date1='2014-03-25 07:30 AM';
 var date2='2014-03-25 04:30 PM';
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
user1851420
  • 455
  • 2
  • 8
  • 19
  • 1
    possible duplicate: http://stackoverflow.com/questions/11170054/compare-two-dates-with-javascript#answer-11170062 – jogesh_pi Mar 25 '14 at 04:50
  • You're going to need to format these date strings differently before you can instantiate new Date objects that work cross-browser. – kinakuta Mar 25 '14 at 04:51

4 Answers4

0

Use getTime:

 if ((new Date(date1).getTime()) > ( new Date(date2).getTime())){
 }
basarat
  • 261,912
  • 58
  • 460
  • 511
  • if i do this alert(new Date(date1).getTime()) i am getting NaN – user1851420 Mar 25 '14 at 05:09
  • That can only happen if `date1` is not what you claim it to be. Can you do `alert(date1)`? – basarat Mar 25 '14 at 05:13
  • the date parsing performed by `new Date` is culture specific on your machine unless the date is in ISO format. If you need to process such a date I recommend looking at moment js parsing capabilities : http://momentjs.com/docs/#/parsing/ – basarat Mar 25 '14 at 09:21
0

Like this -

if((new Date(date1).getTime()) > (new Date(date2).getTime())){
 // do something
}
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
0

Please try this.

if (new Date(date1) > new Date(date2)) 
Raja Danish
  • 235
  • 1
  • 7
  • I have test it, it is working well. i also sending you the scenario in which i have performed testing. function func() { var date1 = '2014-03-25 07:30 AM'; var date2 = '2014-03-25 04:30 PM'; if (new Date(date1) > new Date(date2)) { alert('date1 ' + date1); alert('date2 ' + date2); } else if (new Date(date1) < new Date(date2)) { alert('else date1 ' + date1); alert('else date2 ' + date2); } } – Raja Danish Mar 25 '14 at 05:17
  • i dont know why if i simply do alert(new Date(date1)); i am getting invalid date – user1851420 Mar 25 '14 at 05:56
  • can u pls create a fiddle? – user1851420 Mar 25 '14 at 05:57
  • can u pls check it in firefox? – user1851420 Mar 25 '14 at 06:39
0

This should work:

    var date1 = '2014-03-25 07:30 AM';
    var date2 = '2014-03-25 04:30 PM';
    console.log((new Date(date1)) >= (new Date(date2)));

EDIT: Op correctly pointed out that the code above does not work (in FireFox, for example, but it works in Chrome). I have read that

new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)

are valid instantiations for Date here.

However, the date strings used for date1 and date2 in the example are not valid, therefore not supported in all browsers.

Instead of '2014-03-25 07:30 AM' (which is invalid) you should do one of the following:

  • convert your input into milliseconds
  • convert your input into date string, like "October 13, 1975 11:13:00"
  • calculate the year, month, day, hours, minutes, seconds and milliseconds, then pass them to the instantiation
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175