目前我有在我看來,以下綁定:AngularJS日期過濾器不產生效果
<p>{{claim.date}}</p>
其中包含了這樣的事情:
Oct 19, 2015 4:34:00 PM
我想,使其顯示像這樣格式化:
Oct 19, 2015
看着所有的AngularJS日期過濾器我看到我需要這樣做:
<p>{{claim.date | date : 'mediumDate'}}</p>
但是什麼都沒有發生。我做對了嗎?
目前我有在我看來,以下綁定:AngularJS日期過濾器不產生效果
<p>{{claim.date}}</p>
其中包含了這樣的事情:
Oct 19, 2015 4:34:00 PM
我想,使其顯示像這樣格式化:
Oct 19, 2015
看着所有的AngularJS日期過濾器我看到我需要這樣做:
<p>{{claim.date | date : 'mediumDate'}}</p>
但是什麼都沒有發生。我做對了嗎?
var app = angular.module("App", []);
app.controller("Ctrl", function($scope) {
$scope.date = "Oct 19, 2015 4:34:00 PM";
$scope.date2 = new Date("Oct 19, 2015 4:34:00 PM");
});
app.filter('stringToDate', function() {
return function(input) {
return new Date(input);
};
});
app.filter('stringToDateFormat', function($filter) {
return function(input, format) {
return $filter("date")(new Date(input), format);
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="App" ng-controller="Ctrl">
<div>
{{date | stringToDate |date: "mediumDate"}}
</div>
<div>
{{date | stringToDateFormat: "mediumDate"}}
</div>
<div>
{{date2 | date: "mediumDate"}}
</div>
</body>
我不能實例化一個新的javascipt日期。這是一個字符串變量,我從JSON讀取 – Tiwaz89
然後做$ scope.date = new Date(yourstring);,日期過濾器在日期對象上工作 – IggY
爲什麼不是 app.filter('stringToDate',function($ filter){ return函數(輸入,格式)返回$ filter(「date」)(new Date(input),format); }; }); 那麼你可以避免使用日期過濾器,並使用 {{date | stringToDate:「mediumDate」}} – Kulbhushan
檢查以下片段。它適用於日期對象。確保你的對象是date object
,而不是string
。
angular.module('myApp', []).controller('MyCtrl', function($scope) {
$scope.date = new Date();
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<p>{{date}}</p>
<p>{{date | date : 'mediumDate'}}</p>
</div>
claim.date是日期對象或字符串..? –
我只是想說,我經常使用這個驚人的東西:https://github.com/urish/angular-moment –