我正在尝试使用来自Sql Server的值在我的页面中打印日期和时间。返回日期的格式如下:
/Date(1510563600000)/
的角
$filter('date')(date, format, timezone)
$scope.GetTimelineByUserIdAndProjectId = function (userId, projectId) {
var obj = { "userId": userId, "projectId": projectId }
$http.post('/Admin/GetTimelineByUserIdAndProjectId', obj)
.then(function (response) {
$scope.userTimeline = response.data;
})
}
HTML
<ul class="timeline timeline-inverse" ng-repeat="u in userTimeline">
<li class="time-label">
<span class="bg-red">
{{u.StartDateTime | date:'medium'}}
</span>
</li>
<li>
<i class="fa fa-clock-o bg-gray"></i>
</li>
我希望日期格式为日/月/年小时:分钟。
当我运行这个程序时,我没有定义$ filter,所以我将它放在我的控制器中,如下所示:
var app = angular.module('adminApp', []);
app.controller('adminController', function ($scope, $http, $filter) {
然后它给我一个错误angular.js:14642 ReferenceError: date is not defined
答案 0 :(得分:2)
我假设你得到1510563600000
之类的日期,如果你得到/Date(1510563600000)/
,那么你必须使用javascript在过滤器中简单地将此字符串转换为日期
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl',function($scope, $timeout) {
$scope.varDate='1510563600000';
});
myApp.filter('date',function(){
return function(date,format){
var d=new Date(parseInt(date));
return d.getDate()+'/'+d.getMonth()+'/'+d.getFullYear()+' '+d.getHours()+':'+d.getMinutes()
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
{{varDate | date}}
</div>
答案 1 :(得分:2)
这里我使用了多个过滤器,对你有所帮助。
在您的情况下,您创建的过滤器未链接到您的应用,因此会导致错误
var app = angular.module('adminApp', []);
//$filter('')(date, format, timezone)
app.filter('date_filter',function(){
return function(val){
var n= val.replace('/Date(','').replace(')/','')
return n;
}
})
app.controller('adminController', function ($scope, $http) {
$scope.StartDateTime="/Date(1510563600000)/";
$scope.GetTimelineByUserIdAndProjectId = function (userId, projectId) {
var obj = { "userId": userId, "projectId": projectId }
//$http.post('/Admin/GetTimelineByUserIdAndProjectId', obj)
//.then(function (response) {
//$scope.userTimeline = response.data;
//})
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="adminApp" ng-controller="adminController">
{{StartDateTime|date_filter |date:'medium'}}
<br/>
{{StartDateTime|date_filter |date:'dd/MM/yyyy'}}
<ul class="timeline timeline-inverse" ng-repeat="u in userTimeline">
<li class="time-label">
<span class="bg-red">
{{u.StartDateTime |date_filter| date:'medium'}}
</span>
</li>
<li>
<i class="fa fa-clock-o bg-gray"></i>
</li>
</div>