Sachin HR
Sachin HR

Reputation: 460

Date Comparision in Angularjs ng-if

I am trying to compare two dates Event Date and Current Date.I have two divisions in my HTML page.One is past events in which Event date which has crossed the current date will be automatically placed into Past events div.Another division is Upcoming Events in which event date is greater than current date. Condition I am using is

<data-ng-if="(event.eventDate | date:'yyyy-dd-MM  HH:mm:ss')>
        (todayDate | date:'yyyy-dd-MM HH:mm:ss')">

If it is true the added event will be placed into Upcoming events div section.Otherwise,

<data-ng-if="(event.eventDate | date:'yyyy-dd-MM  HH:mm:ss')<
        (todayDate | date:'yyyy-dd-MM HH:mm:ss')">

Then Added event will be placed into Past events section. In Controller.js I am writing function

$scope.init = function() {
    $scope.todayDate = new Date();
}

But right now it is comparing only the day.For example if today date is 2017-07-27 and if I enter 2017-06-30 while adding events then it should go to Past Events section but it is getting added in Upcoming events section only because it is comparing only day that is 30 and 27 as in the example.And if suppose today date is 2017-07-27 and if i add event date as 2017-08-20 then it should be placed in Upcoming events but it gets added into past section.Can anyone tell me how to achieve this with yyyy-MM-dd HH:MM:SS format?It has to compare time also.

Upvotes: 1

Views: 9104

Answers (1)

devqon
devqon

Reputation: 13997

When comparing dates you shouldn't use the string notations. You can just compare the date objects itself:

<data-ng-if="event.eventDate < todayDate">

Upvotes: 6

Related Questions