简体   繁体   中英

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.

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM