大家好我正在使用angularjs我有一组数据并在一个范围变量中得到它现在我需要的是我需要根据日期排序获取数组值' MONTH'我的日期格式DispenseMonth
":" 9/1 2016"(MM / DD / YY)这个我的格式在这里我附上小提琴现在我需要的是我得到了数组顺序基于DispenseMonth
-orderby Month帮助如何执行此操作
答案 0 :(得分:2)
使用日期对象中的getMonth()
进行排序。
var app = angular.module("Filter", []);
app.controller('StateShowCtrl', ['$scope', '$http',
function ($scope, $http) {
var data=[
{
"PatientState": "AK",
"DispenseMonth": "7/1/2016"
},
{
"PatientState": "AK",
"DispenseMonth": "8/1/2016"
},
{
"PatientState": "AK",
"DispenseMonth": "9/1/2016"
},
{
"PatientState": "AK",
"DispenseMonth": "10/1/2016"
},
{
"PatientState": "AK",
"DispenseMonth": "11/1/2016"
},
{
"PatientState": "AK",
"DispenseMonth": "2/2/2017"
},
{
"PatientState": "AK",
"DispenseMonth": "3/5/2017"
}
]
$scope.StateInformations =data.sort(function(a, b) {
return new Date(a.DispenseMonth).getMonth() - new Date(b.DispenseMonth).getMonth();
});
console.log( $scope.StateInformations);
}
]);

<!DOCTYPE html>
<html ng-app="Filter">
<head>
<!-- basic scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body class="no-skin" ng-controller="StateShowCtrl">
</body>
&#13;
答案 1 :(得分:1)
试试这个:
var app = angular.module("Filter", []);
app.controller('StateShowCtrl', ['$scope', '$http', function ($scope, $http) {
var data=[
{
"PatientState": "AK",
"DispenseMonth": "7/1/2016"
},
{
"PatientState": "AK",
"DispenseMonth": "8/1/2016"
},
{
"PatientState": "AK",
"DispenseMonth": "9/1/2016"
},
{
"PatientState": "AK",
"DispenseMonth": "10/3/2016"
},
{
"PatientState": "AK",
"DispenseMonth": "11/1/2016"
},
{
"PatientState": "AK",
"DispenseMonth": "2/1/2017"
},
{
"PatientState": "AK",
"DispenseMonth": "3/1/2017"
}]
$scope.StateInformations =data;
//console.log( $scope.StateInformations);
$scope.add = function(){
$scope.StateInformations.push({
"PatientState": "AK",
"DispenseMonth": "3/2/2017"
});
}
}
]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app='Filter' class="no-skin" ng-controller="StateShowCtrl">
<input type='button' ng-click='add()' value='addNew'/>
<ul ng-init='item.month=+item.DispenseMonth.split("/")[1]' ng-repeat='item in StateInformations | orderBy:"month"' >
<li>{{item | json}}</li>
</ul>
</div>
&#13;
答案 2 :(得分:1)
据我了解,您只对分拣月份感兴趣,因此您可以应用这个非常短的分拣功能:
data.sort( (a,b) => a.DispenseMonth.split('/')[0] - b.DispenseMonth.split('/')[0]).