ng-if日期比较而不是angularjs中的年份

时间:2015-09-11 10:46:01

标签: javascript angularjs asp.net-mvc

我有2个日期,一个是第一天的第一天,另一个来自数据库(在json对象中),当比较为ng-如果它忽略年份。它只比较日期和月份

这是我的代码

  <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 个答案:

答案 0 :(得分:1)

您只需将日期格式更改为yyyy/MM/dd,所有内容似乎都可以正常工作

&#13;
&#13;
// Code goes here

angular.module("app",[]).controller('ctrl',function($scope){
  $scope.startDate = '/Date(1441045800000)/';
  $scope.mfstDate = new Date(2015,8,1);
})
&#13;
<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>
&#13;
&#13;
&#13;

更新我认为你需要修改你的getFirst_Last_DateOfMonth函数,你不需要手动格式化字符串,因为angular有一个很好的过滤器date,你已经在视图中看到了

所以你可以只返回日期而不是字符串,

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
}

请参阅下面的代码段

&#13;
&#13;
// 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);
})
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我建议在控制器中对函数中的日期进行比较,并在ng-if中调用函数。为了正确比较日期,您应该使用Date object

所以它看起来像这样:

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

虽然功能做了这样的事情:

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

   return (dateObj1 > dateObj2);
}