2017-02-24 18 views
0

日付は/Date(1451602800000)/です。AngularJsの解析日

この日付を「短い」形式で表示したいと考えています。

私は{{myDate | date:'short'}}を試みたが、結果は/Date(1451602800000)/

+2

可能な複製(http://stackoverflow.com/questions/27584890/angular-date-parsing-unexpected-output) –

答えて

0

あり、ここで、この

angular.module("app",[]) 
 
.controller("ctrl",function($scope){ 
 
\t $scope.date = "\/Date(1451602800000)\/"; 
 
}) 
 
.filter("mydate", function() { 
 
    var re = /\/Date\(([0-9]*)\)\//; 
 
    return function(x) { 
 
     var m = x.match(re); 
 
     if(m) return new Date(parseInt(m[1])); 
 
     else return null; 
 
    }; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="ctrl"> 
 
    {{date |mydate }} <br> 
 
    {{date |mydate | date: 'yyyy-MM-dd HH:mm' }} 
 
</div>

1

のような、カスタマーフィルタを使用すると、別のアプローチです。
私はJSで@sachila ranawaka
から正規表現を使用します。

.filter('regexp', function(){ 
    return function(input){ 
     return input.match(/\/Date\(([0-9]*)\)\//)[1]; 
    }; 
}); 

HTMLで:

{{ myDate | regexp | date:'short' }} 

このようにあなたの好みに角度日付フィルタを使用することができます。

参照:[予期しない出力を解析アンギュラ日付]のhttps://docs.angularjs.org/api/ng/filter/date