Angular:日期过滤器添加时区,如何输出UTC? [英] Angular: date filter adds timezone, how to output UTC?
问题描述
我正在使用日期过滤器以某种格式呈现 unix 时间戳.我注意到过滤器将本地时区添加到输出中.
有没有什么办法可以简单地输出确切的时间戳,而不添加任何时区信息?
输入:
talk.content.date_and_time = 1400167800
(是 05/15/14 @ 3:30:00pm UTC)
代码:
{{talk.content.date_and_time*1000 |日期:'dd-M-yyyy H:mm Z'}}
输出:
15-5-2014 17:30 +0200
如何将输出设为 15:30 而不是 17:30?
'Z' 用于添加时区信息.至于输出 UTC,这似乎是一些混淆的主题——人们似乎倾向于 moment.js.
借用这个答案,你可以在没有 moment.js 的情况下做这样的事情:
控制器
var app1 = angular.module('app1',[]);app1.controller('ctrl',['$scope',function($scope){var toUTCDate = 函数(日期){var _utc = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());返回_utc;};var millisToUTCDate = function(millis){返回到UTCDate(新日期(毫秒));};$scope.toUTCDate = toUTCDate;$scope.millisToUTCDate = millisToUTCDate;}]);
模板
<头><script data-require="angular.js@*" data-semver="1.2.12" src="http://code.angularjs.org/1.2.12/angular.js"></script><link rel="stylesheet" href="style.css"/><script src="script.js"></script>头部><身体><div ng-controller="ctrl"><div>UTC {{millisToUTCDate(1400167800) |日期:'dd-M-yyyy H:mm'}}
<div>本地 {{1400167800 |日期:'dd-M-yyyy H:mm'}}
</html>
这里是 plunker 玩它
另请注意,使用此方法,如果您使用 Angular 日期过滤器中的Z",它似乎仍会打印您的本地时区偏移量.
I'm using the date filter to render a unix timestamp in a certain format. I've noticed the filter adds the local timezone to the output.
Is there any way to simply output the exact timestamp, without adding any timezone information?
Input:
talk.content.date_and_time = 1400167800
(is 05 / 15 / 14 @ 3:30:00pm UTC)
Code:
{{talk.content.date_and_time*1000 | date:'dd-M-yyyy H:mm Z'}}
Output:
15-5-2014 17:30 +0200
How can I make the output 15:30 instead of 17:30?
The 'Z' is what adds the timezone info. As for output UTC, that seems to be the subject of some confusion -- people seem to gravitate toward moment.js.
Borrowing from this answer, you could do something like this without moment.js:
controller
var app1 = angular.module('app1',[]);
app1.controller('ctrl',['$scope',function($scope){
var toUTCDate = function(date){
var _utc = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
return _utc;
};
var millisToUTCDate = function(millis){
return toUTCDate(new Date(millis));
};
$scope.toUTCDate = toUTCDate;
$scope.millisToUTCDate = millisToUTCDate;
}]);
template
<html ng-app="app1">
<head>
<script data-require="angular.js@*" data-semver="1.2.12" src="http://code.angularjs.org/1.2.12/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<div ng-controller="ctrl">
<div>
utc {{millisToUTCDate(1400167800) | date:'dd-M-yyyy H:mm'}}
</div>
<div>
local {{1400167800 | date:'dd-M-yyyy H:mm'}}
</div>
</div>
</body>
</html>
here's plunker to play with it
Also note that with this method, if you use the 'Z' from Angular's date filter, it seems it will still print your local timezone offset.
这篇关于Angular:日期过滤器添加时区,如何输出UTC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!