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

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

问题描述

我正在寻找相对于用户时区的日期。



我希望Angular能够全局配置 Date 过滤器来完成此操作 - 手动执行此操作我们的时间戳已经包裹在一个 timestamp()函数中。 (简单地乘以1000),但是如果我没有 ,我宁愿不修改该功能。



编辑:



我正在做这件事,它的工作原理,但如上所述,我想将这一级设置为更高,如果可能

  $ 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());
返回邮票;
};


解决方案

所以,感谢角1.4.x的更改现在是微不足道的。处理这个的正确方法是创建一个修改器,在运行之前更改内置日期过滤器。这很简单,不会对性能产生影响。



这是我使用的一个。如果没有指定时区,它只需添加一个DEFAULT_TIMEZONE。只要没有其他时区可以将应用程序中的所有日期移动到GMT。

  module.config( ['$ provide',function($ provide){
var DEFAULT_TIMEZONE ='GMT';

$ offers.decorator('dateFilter',['$ delegate','$ inject' ,function($ delegate,$ inject){
var oldDelegate = $ delegate;

var standardDateFilterInterceptor = function(date,format,timezone){
if(angular.isUndefined时区)){
timezone = DEFAULT_TIMEZONE;
}
return oldDelegate.apply(this,[date,format,timezone]);
};

return standardDateFilterInterceptor;
}]);
}]);


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;
};

解决方案

So, thanks to changes in angular 1.4.x this is now trivial. The proper way to handle this would be to create a decorator that alters the built in date filter before it runs. This is trivially easy, and won't have an impact on performance.

This is one I use. It simply adds a DEFAULT_TIMEZONE if no timezone is specified. This has the effect of moving all dates in the app to GMT as long as no other timezone is given.

module.config(['$provide', function($provide) {
     var DEFAULT_TIMEZONE = 'GMT';

     $provide.decorator('dateFilter', ['$delegate', '$injector', function($delegate, $injector) {
       var oldDelegate = $delegate;

       var standardDateFilterInterceptor = function(date, format, timezone) {
         if(angular.isUndefined(timezone)) {
           timezone = DEFAULT_TIMEZONE;
         }
         return oldDelegate.apply(this, [date, format, timezone]);
       };

       return standardDateFilterInterceptor;
     }]);
}]);

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

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