gpt4 book ai didi

javascript - ng-if AngularJS中的日期比较不是年份

转载 作者:行者123 更新时间:2023-11-27 23:53:07 28 4
gpt4 key购买 nike

在 ng-if 中比较忽略年份时,我有 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>

最佳答案

您只需将日期格式更改为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函数,你不需要手动格式化字符串,因为Angular有一个很好的过滤器date您已经在 View 中了。

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

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>

关于javascript - ng-if AngularJS中的日期比较不是年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32521617/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com