2017-12-07 24 views
1

SQL Serverからの値を使用してページに日付と時刻を印刷しようとしています。返される日付の書式はそうのようなものです:anglejsで日付が定義されていません

/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> 

日付形式を日/月/年時:分にしたい。

私はこのプログラムを実行すると、私は$フィルタが、私はこのように私のコントローラに置くように定義されていないしまった。

var app = angular.module('adminApp', []); 
app.controller('adminController', function ($scope, $http, $filter) { 

をしかし、それは、私が私にangular.js:14642 ReferenceError: date is not defined

+0

の原因となるよう

は、あなたのケースでは、あなたのアプリにリンクされていないフィルタを作成していますか? datetimeデータ型を使用しませんでしたか? – JkAlombro

+0

私のSqlServerの日付形式はdatetimeです。 –

答えて

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>

+0

ありがとうございます。魅力のように動作します。 –

+0

いいね!.... –

2

の誤差を与えますもしあなたが/Date(1510563600000)/のようになったら、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>

関連する問題