1

I have 2 dates one is first day of month and other is from database (in json object) when compare in ng-if its ignoring year. It comparing date and month only

Here is my code

  <tr ng-repeat="c in listBCust | orderBy : orderByField | filter : srchInv" ng-class="{'holdCust':'01/09/2015'>(c.startDate.slice(6,-2) | date:'dd/MM/yyyy')}">

    <td>  
       <div ng-if="'01/09/2015'>(c.startDate.slice(6,-2) | date:'dd/MM/yyyy')" style="background-color:#E25D5D">{{c.startDate.slice(6,-2) | date:'dd/MM/yyyy'}}</div>

       <div ng-if="'01/09/2015'<(c.startDate.slice(6,-2) | date:'dd/MM/yyyy')" style="background-color:#E25D5D">{{c.startDate.slice(6,-2) | date:'dd/MM/yyyy'}}</div>
       <div>{{MfstDate}}</div>
    </td>

  </tr>

2 Answers2

1

You can just change date format to yyyy/MM/dd and all seems work

// Code goes here

angular.module("app",[]).controller('ctrl',function($scope){
  $scope.startDate = '/Date(1441045800000)/';
  $scope.mfstDate = new Date(2015,8,1);
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
    <h1>Hello Plunker!</h1>
    {{startDate.slice(6,-2) | date:'dd/MM/yyyy'}}
    <div ng-if="(mfstDate| date:'yyyy/MM/dd')>(startDate.slice(6,-2) | date:'yyyy/MM/dd')" style="background-color:#E25D5D">{{startDate.slice(6,-2) | date:'dd/MM/yyyy'}} less {{mfstDate| date:'dd/MM/yyyy'}}</div>
    <div ng-if="(mfstDate| date:'yyyy/MM/dd')<(startDate.slice(6,-2) | date:'yyyy/MM/dd')" style="background-color:#E25D5D">{{startDate.slice(6,-2) | date:'dd/MM/yyyy'}} greater {{mfstDate| date:'dd/MM/yyyy'}}</div>
  </div>

UPDATE i think you need a bit fix your getFirst_Last_DateOfMonth function, you not need manually format string, because angular have a good filter date that you already yse inside view.

So you can return just date instead string,

function getFirst_Last_DateOfMonth(F_L) {
    //here F=First Date of Current month
    //     L=Last Date of Current Month

    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth();

    var yyyy = today.getFullYear();

    var firstLastDay = F_L == 'F'? new Date(yyyy,mm,1) : new Date(yyyy, mm + 1, 0);
    return firstLastDay; //return Date object and in view use date filter when need comaring
    //return $filter('date')(firstLastDay,'yyyy/MM/dd'); //uncomment if want return formatted string
}

See snippet below

// Code goes here

angular.module("app", []).controller('ctrl', function($scope, $filter) {
  function getFirst_Last_DateOfMonth(F_L, formatString) {
    //here F=First Date of Current month
    //     L=Last Date of Current Month

    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth();

    var yyyy = today.getFullYear();

    var firstLastDay = F_L == 'F' ? new Date(yyyy, mm, 1) : new Date(yyyy, mm + 1, 0);
    return !formatString ? firstLastDay //return Date object and in view use date filter when need comaring
      : $filter('date')(firstLastDay, 'yyyy/MM/dd'); //uncomment if want return formatted string
  }

  $scope.startDate = '/Date(1441045800000)/';
  $scope.mfstDate = getFirst_Last_DateOfMonth('F');

  $scope.mfstDateString = getFirst_Last_DateOfMonth('F', true);
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <h1>Hello Plunker!</h1>
  {{startDate.slice(6,-2) | date:'dd/MM/yyyy'}}
  <div>
    mfstDate is date!
    <div ng-if="(mfstDate| date:'yyyy/MM/dd')>(startDate.slice(6,-2) | date:'yyyy/MM/dd')" style="background-color:#E25D5D">{{startDate.slice(6,-2) | date:'dd/MM/yyyy'}} less {{mfstDate| date:'dd/MM/yyyy'}}</div>
    <div ng-if="(mfstDate| date:'yyyy/MM/dd')<(startDate.slice(6,-2) | date:'yyyy/MM/dd')" style="background-color:#E25D5D">{{startDate.slice(6,-2) | date:'dd/MM/yyyy'}} greater {{mfstDate| date:'dd/MM/yyyy'}}</div>
  </div>
  <div>
    mfstDate is formatted string!
    <div ng-if="mfstDateString>(startDate.slice(6,-2) | date:'yyyy/MM/dd')" style="background-color:#E25D5D">{{startDate.slice(6,-2) | date:'dd/MM/yyyy'}} less {{mfstDateString}}</div>
    <div ng-if="mfstDateString<(startDate.slice(6,-2) | date:'yyyy/MM/dd')" style="background-color:#B8BCEF">{{startDate.slice(6,-2) | date:'dd/MM/yyyy'}} greater {{mfstDateString}}</div>
  </div>
</div>
Grundy
  • 13,356
  • 3
  • 35
  • 55
  • thanks for this solution. but its not working when i replace this fix date with dynamic date like ng-if="(MfstDate | date:'yyyy/MM/dd'>(startDate.slice(6,-2) | date:'yyyy/MM/dd')" .....Please tell me solution – Satyender Saini Sep 12 '15 at 11:00
  • @SatyenderSaini, what is `MfstDate`? can you provide _plunkr_ how you get and use it? – Grundy Sep 12 '15 at 12:05
  • @SatyenderSaini, so, in plunkr you use your old format `dd/MM/yyyy`, if you change it to `yyyy/MM/dd` and also if you fix how you fill `mfstDate` - all will be work: just use `yyyy/MM/dd` format, and return from `getFirst_Last_DateOfMonth` not string but date. as you can see in code snippet: all work fine in this case – Grundy Sep 12 '15 at 13:36
  • @SatyenderSaini, see updated answer, and new code snippet – Grundy Sep 12 '15 at 13:54
0

I would suggest to make the comparison of dates in a function in your controller, and call the function in your ng-if. And in order to compare dates correctly, you should use the Date object.

So it would look something like this:

<div ng-if="compareDates(date1, date2)">*content*</div>

While the function doing something like this:

function compareDates(date1, date2) {
   var dateObj1 = new Date(date1);
   var dateObj2 = new Date(date2);

   return (dateObj1 > dateObj2);
}
Loukas Avramidis
  • 519
  • 3
  • 10
  • 24
  • not working i have used
    – Satyender Saini Sep 11 '15 at 11:08
  • That why I am said that you should use Date objects. new Date('01/09/2015') < new Date(c.startDate) or something. | date: 'dd/MM/yyyy' is a filter, it doesn't work that way... – Loukas Avramidis Sep 11 '15 at 11:10
  • @AwawzaMechaschwawza, what you mean _date: 'dd/MM/yyyy' is a filter, it doesn't work that way_? – Grundy Sep 11 '15 at 11:13
  • Please ignore first comment...i have even tried this
    – Satyender Saini Sep 11 '15 at 11:17
  • @Grundy I mean that using (c.startDate.slice(6,-2) | date:'dd/MM/yyyy') will compare the dates as strings. You need Date objects in order to properly compare date values. – Loukas Avramidis Sep 11 '15 at 11:18
  • @AwawzaMechaschwawza, but in `ng-if` compared just strings – Grundy Sep 11 '15 at 11:28
  • @SatyenderSaini Dont use MfstDate | date: 'dd/MM/yyyy' as a parameter, change the format in the function itself. And for the date comparison, you cant go wrong if you simply use Date objects. Take a look at this https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – Loukas Avramidis Sep 11 '15 at 11:29
  • I can use date object in controller only. how to compare dates on view – Satyender Saini Sep 11 '15 at 11:40
  • ng-if can be used with a function in your controller, and it is actually recommended that way. I showed you in my answer man... ng-if="function()" The function will have to return true/false. – Loukas Avramidis Sep 11 '15 at 11:45
  • I used as you recommended but did not work. i tried ng-if="(MfstDate | date:'dd/MM/yyyy'),(c.startDate.slice(6,-2) | date:'dd/MM/yyyy')" or you suggesst me what to pass in parameter ng-if="compareDates(whatToPassHere,whatToPassHere)" – Satyender Saini Sep 11 '15 at 12:34
  • ng-if="compareDates(MfstDate ,c.startDate.slice(6,-2))" then put those values Date object (f.e. new Date(mfstDate)) and compare them as you normally would. – Loukas Avramidis Sep 11 '15 at 13:03