目前我认为有以下绑定(bind):
<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>
关于javascript - AngularJS 日期过滤器不产生结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33387380/