2017-02-10 79 views
0

我正在寻找以毫秒输出日期,但它不工作。使用getTime()的日期(毫秒)

在HTML文件

我有:

{{product.date_expiration}}://至极输出==> 2018-01-13T12:22:06.165Z

{{的currentdate}}: //至极输出==> “2017-02-10T13:18:58.339Z”

与:

date_expiration: {type: Date} 

和:

{CurrentDate}}: {type: Date} 

whene我试图让日期以毫秒为单位:

{{product.date_expiration.getTime()}} ==>不输出任何

和:

{{CurrentDate.getTime()}} ==> output 1486733469830 

任何帮助将不胜感激。感谢

+0

date_expiration可能是一个字符串,而不是日期。 –

+0

是否{{produit.date_expiration.getTime()}}是问题中的拼写错误,还是错误原因! –

+1

sory,这是一个错字,后编辑 – Nassim

回答

0

好像date_expiration不object.So getTime()函数未在it.We工作的日期已经使用过滤器将其转换成Date对象&给我们以毫秒为单位的时间,因为我们不能在角表达式中使用Date()

var app = angular.module('myApp', []); 
 
app.filter('inMilliseconds', function() { 
 
    return function(x) { 
 
       
 
     return new Date(x).getTime(); 
 
    }; 
 
}); 
 
app.controller('myCtrl', function($scope) { 
 
    $scope.date_expiration = "2018-01-13T12:22:06.165Z"; 
 
    $scope.currentDate = new Date(); 
 
});
<!DOCTYPE html> 
 
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<body> 
 

 
<div ng-app="myApp" ng-controller="myCtrl"> 
 
date_expiration: {{date_expiration | inMilliseconds }} 
 
<br> 
 
currentDate: {{currentDate.getTime() }} 
 
</div> 
 

 
<script> 
 

 
</script> 
 

 
</body> 
 
</html>