13

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?

2
  • 1
    I'm assuming those segments are actually strings? Commented Oct 18, 2014 at 1:04
  • They are stored as date objects in a mongoDB and pulled in my controller. I was just trying to show how the data looked. Commented Oct 18, 2014 at 1:06

2 Answers 2

13

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

Sign up to request clarification or add additional context in comments.

4 Comments

okay that definitely answered my question. I think my problem is that the date objects coming from mongo are coming back as strings and not as objects but that may be because it was test data I entered manually and did it wrong.
@DaltonShehan You can just make it into a new date object with the string like this: new Date('2006-01-01T04:00:00.000Z')
Right that what I ended up doing thanks for the help.
@DaltonShehan you can compare strings with inequalities, and you should get the same result as a comparison of a date with an inequality if the string is formatted the same for both dates.
8

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.