角:日期过滤器添加时区,如何输出UTC? [英] Angular: date filter adds timezone, how to output UTC?
问题描述
我使用日期过滤器来呈现特定格式Unix时间戳。我注意到过滤器将本地时区到输出。
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?
输入:
talk.content.date_and_time = 1400167800
(是05年/14分之15@下午3时三十分00秒UTC)
(is 05 / 15 / 14 @ 3:30:00pm UTC)
code:
{{talk.content.date_and_time*1000 | date:'dd-M-yyyy H:mm Z'}}
输出:
15-5-2014 17:30 +0200
我怎样才能使输出的15:30,而不是17:30?
How can I make the output 15:30 instead of 17:30?
推荐答案
在'Z'是增加了时区信息。至于输出UTC,这似乎是有些混乱的主题 - 人似乎倾向于 moment.js 。
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.
借款<一个href=\"http://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc#answer-6777470\">answer,你可以做这样的事情,而不moment.js:
Borrowing from this answer, you could do something like this without moment.js:
控制器
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;
}]);
模板
<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>
下面的 plunker 发挥它
又见<一个href=\"http://stackoverflow.com/questions/20662140/using-angularjs-date-filter-with-utc-date?lq=1\">this和<一个href=\"http://stackoverflow.com/questions/20470139/angularjs-timestamp-to-formatted-utc-time-with-date-helper\">this.
另外请注意,使用这种方法,如果您从角的日期过滤器中使用的'Z',现在看来,这仍然会打印您的本地时区的偏移量。
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.
这篇关于角:日期过滤器添加时区,如何输出UTC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!