javascript 如何全局设置角力矩时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25283901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set angular moment timezone globally?
提问by Dino
I used angular moment js on my angular app. I want to show date and time according to the time zone of the current user. I'm unable to apply time zone globally on my app.
我在我的角度应用程序上使用了角度力矩 js。我想根据当前用户的时区显示日期和时间。我无法在我的应用程序上全局应用时区。
https://github.com/urish/angular-moment
https://github.com/urish/angular-moment
I tried this.
我试过这个。
angular.module('myapp').constant('angularMomentConfig', {
timezone: 'Europe/London' // e.g. 'Europe/London'
});
In one of my view -
在我看来——
{{home.eventStart| amDateFormat:'dddd, MMMM Do YYYY, h:mm:ss a'}}
In one of my controller -
在我的一个控制器中 -
$scope.profile.eventStart = moment(data.start).format('YYYY-MM-DD hh:mm a');
I'm not getting any changes in both cases, even after I set the time zone. Am I missing something ?
即使在我设置了时区之后,我在这两种情况下都没有得到任何更改。我错过了什么吗?
回答by CKK
You cannot change constant after you set it. As injections are not dependent on module.constant or module value, this should do the trick:
设置后无法更改常量。由于注入不依赖于 module.constant 或 module 值,这应该可以解决问题:
angular.module('myapp').value('angularMomentConfig', {
timezone: 'Europe/London' // e.g. 'Europe/London'
});
回答by Firze
Since version 1.0.0-beta.1, time zone can be set using amMomentservice. Just inject the amMoment service and call changeTimeZone:
从 1.0.0-beta.1 版本开始,可以使用amMoment服务设置时区。只需注入 amMoment 服务并调用 changeTimeZone:
amMoment.changeTimezone('Europe/London');
回答by Matt Johnson-Pint
The timezone
parameter is optional. If you omit it, it will use the time zone of the local machine automatically. That is the default behavior of moment.js and of the JavaScript Date
object.
该timezone
参数是可选的。如果省略它,它将自动使用本地机器的时区。这是 moment.js 和 JavaScriptDate
对象的默认行为。
回答by Mayur Padshala
I too had the same problem.
我也有同样的问题。
This is how to solve this out.
这就是解决这个问题的方法。
You need to include moment-timezone.jsv0.3.0 or greater in your project, otherwise the custom timezone functionality will not be available.
您需要在项目中包含moment-timezone.jsv0.3.0 或更高版本,否则自定义时区功能将不可用。
回答by poulp
app.run(['amMoment',
function (amMoment) {
amMoment.changeTimezone('Europe/Paris');
}
]);