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';
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';
Use getTime
:
if ((new Date(date1).getTime()) > ( new Date(date2).getTime())){
}
Like this -
if((new Date(date1).getTime()) > (new Date(date2).getTime())){
// do something
}
Please try this.
if (new Date(date1) > new Date(date2))
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: