[asp.net-mvc]吴-如果日期比较不年 angularjs

发布时间: 2017/3/25 1:47:54
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有 2 日期,一个是第一个月和其他的一天是从数据库中 (在 json 对象) 时比较在吴-如果其忽略年。它比较日期和月份只

这里是我的代码

  <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>

解决方法 1:

你可以只是更改日期格式为 yyyy/MM/dd ,一切似乎都要工作

// 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>

更新我觉得你有点需要修复你 getFirst_Last_DateOfMonth 功能,您不需要手动设置格式的字符串,因为角有很好的过滤 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
}

请参阅下面的片断

// 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>

官方微信
官方QQ群
31647020