AngularJs ng-if comparing dates

时光怂恿深爱的人放手 提交于 2019-12-22 02:01:16

问题


I am looking to compare two dates in ng-if this is what my jade file looks like.

li.list-group-item(ng-if="app.Segments[0].StartDate.getTime() > date.getTime()")
    div.row
        div.col-xs-12
            span
                i.fa.fa-plus-square

This code will hopefully add an li to my ui if the startdate of the first segment is after today.

$scope.date = new Date();
$scope.app = {
    Segments: [{
       StartDate: 2014-11-15T04:00:00.000Z
       EndDate: 2014-11-20T04:00:00.000Z
    },
    {
        StartDate: 2014-11-21T04:00:00.000Z
        EndDate: 2014-11-25T04:00:00.000Z
    }]

}

Is there anyway to make this work?


回答1:


If they're date object just compare them directly without .getTime()

$scope.date2005 = new Date('2005/01/01');
$scope.date2006 = new Date('2006-01-01T04:00:00.000Z');
<div ng-if="date2005 > date2006">date2005 > date2006</div> <!-- won't show -->
<div ng-if="date2006 > date2005">date2006 > date2005</div> <!-- will show -->

http://plnkr.co/edit/fNB11U6KmFszdV3lMAPJ?p=preview




回答2:


Angular can directly compare date if they are well formated:

view

li.list-group-item(ng-if="app.Segments[0].StartDate > now")

controller

$scope.now = new Date();
$scope.now.toISOString();


来源:https://stackoverflow.com/questions/26435313/angularjs-ng-if-comparing-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!