Search code examples
javascriptangularjsdate-comparison

Date comparison using $filter not working


I have an application in Angular JS and on one screen, there are 2 date pickers and there is a validation that FirstDate cannot be after SecondDate.

Test case:

FirstDate: '10/01/2014'
SecondDate: '04/01/2015'

Condition:

if( $filter('date')(FirstDate) > $filter('date')(SecondDate) )
  alert(FirstDate)
else
  alert(SecondDate)

Desired Output:

"SecondDate"

Output:

"FirstDate"

Can you please help what the issue is? My guess is, instead of considering date, it is considering them as string.


Solution

  • $filter('date') works with date object and converting it to string. So in you case you rather need to convert string to date object before you compare it

    if( Date.parse(FirstDate) > Date.parse(SecondDate) )
      alert(FirstDate);
    else
      alert(SecondDate);