AngularJS全球日期时区偏移 [英] AngularJS global Date timezone offset

查看:2619
本文介绍了AngularJS全球日期时区偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待展示相对于用户的时区的日期。

我的希望是角有办法全局配置的日期过滤器要做到这一点,需要手工做这在逐案基础上的感觉错了。

我的时间戳已经包裹在一个时间戳()功能(只需1000乘),但我preFER如果不修改函数I不要的有无的到。

编辑:

我这样做,和它的作品,但如上所述,我想设置这个水平如果可能的话

  $ scope.timestamp =功能(unix_time){
    VAR划时代=(unix_time * 1000);
    VAR日期=新的日期();
    变种localOffset =(-1)* date.getTimezoneOffset()* 60000;
    VAR印花税= Math.round(新日期(纪元+ localOffset).getTime());
    返回印花税;
};


解决方案

从官方文档:


  

时区


  
  记

请即角日期时间过滤器使用的时区设置
  的浏览器。因此相同的应用程序将显示不同的时间
  根据计算机的时区设置信息
  在应用程序运行上。无论是使用Javascript也不角
  目前支持显示由指定的时区的日期
  开发者。


http://docs.angularjs.org/guide/i18n

A级更高可以创建一个包装的键入的(AKA级,...)与功能构造。这应该是在为可能被到处使用它的应用程序的入口点$ C $的CD。

I'm looking to show dates relative to the users' timezones.

My hope is that Angular has way to globally config the Date filter to do this—having to do this manually on a case-by-case basis feels wrong.

My timestamps are already wrapped in a timestamp() function (simply to multiply by 1000), but I'd prefer not to modify that function if I don't have to.

Edit:

I'm doing this, and it works, but as stated above, I'd like to set this one level higher if possible

$scope.timestamp = function (unix_time) {
    var epoch = (unix_time * 1000);
    var date = new Date();
    var localOffset = (-1) * date.getTimezoneOffset() * 60000;
    var stamp = Math.round(new Date(epoch + localOffset).getTime());
    return stamp;
};

解决方案

From official documentation:

Timezones

Keep in mind that Angular datetime filter uses the time zone settings of the browser. So the same application will show different time information depending on the time zone settings of the computer that the application is running on. Neither Javascript nor Angular currently supports displaying the date with a timezone specified by the developer.

http://docs.angularjs.org/guide/i18n

A level 'higher' could be creating a wrapper type (AKA class, ...) with your function as constructor. This should be coded at the entry point of the application for being possible using it everywhere.

这篇关于AngularJS全球日期时区偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆