如何在AngularJS中将毫秒转换为格式化DateTime

时间:2017-04-18 07:38:08

标签: angularjs json datetime formatting

从服务器反序列化JSON后,我得到了以毫秒为单位的日期时间:$scope.JDT = "1492499995056";。我可以显示范围变量' JDT'在我的视图上使用过滤器:

{{JDT |日期:" dd / MM / yyyy h:mm:ss a"}}

...但是,我真的不需要它。我想在我的范围内使用这个过滤器,用这样的格式化数据填充另一个范围变量,但我找不到办法。

2 个答案:

答案 0 :(得分:1)

在控制器中使用这样的过滤器

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Retirement Date</title> <link href="jquery-ui.css" rel="stylesheet"> </head> <body> <p>DOB: <input type="text" id="dob"></p> <p>DORetirement: <input type="text" id="doret"></p> <script src="jquery.js"></script> <script src="jquery-ui.js"></script> <script> $( function() { $('#dob').datepicker({ onSelect: function(value) { var retirementDate = new Date(value); var lastDay = new Date(retirementDate.getFullYear()+58, retirementDate.getMonth() + 1, 0); var lastDayWithSlashes = (lastDay.getMonth() + 1) + '/' + (lastDay.getDate()) + '/' + lastDay.getFullYear(); $('#doret').val(lastDayWithSlashes); }, yearRange: "-100:+0", // last hundred years dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true }); } ); </script> </body> </html>

演示

&#13;
&#13;
$scope.sample = $filter('date')($scope.JDT,"dd/MM/yyyy h:mm:ss a");
&#13;
angular.module("app",[])
.controller("ctrl",function($scope,$filter){
$scope.JDT = "1492499995056"

$scope.sample = $filter('date')($scope.JDT,"dd/MM/yyyy h:mm:ss a");
console.log($scope.sample)

})
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以在控制器中注入$filter,并具有以下内容:

$scope.someVariable = $filter('date')($scope.JDT, 'dd/MM/yyyy h:mm:ss a')