1

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.

Talha Awan
  • 4,573
  • 4
  • 25
  • 40
Sachin HR
  • 450
  • 11
  • 28

1 Answers1

6

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

<data-ng-if="event.eventDate < todayDate">
devqon
  • 13,818
  • 2
  • 30
  • 45
  • But my confusion is if we dont specify the format how it is comapring the time also? – Sachin HR Jul 27 '17 at 12:11
  • Because it is just a date object. A date consists of date and time, and one object with higher time is natively greater than a same date with earlier time. See [this answer also](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – devqon Jul 27 '17 at 12:15
  • How can I compare it to a string? – ScipioAfricanus Dec 20 '20 at 22:52