【问题标题】:Angularjs display timestamp date in different timezonesAngularjs在不同时区显示时间戳日期
【发布时间】:2015-05-09 01:05:40
【问题描述】:

只需使用date:'medium',我就可以正确地将选定的时间戳显示为该时间戳的本地时间。

我想要实现的是在某个时区显示这个时间戳,例如 UTC+03:00 或 GMT+03:00 只是举个例子。

我试过用

  • 日期:'中':'UTC+03:00'
  • 日期:'中':'UTC+0300'
  • 日期:'中':'GMT+03:00'
  • 日期:'中':'GMT+0300'
  • 日期:'中':'+03:00'
  • 日期:'中':'+0300'

这些似乎都没有将时间戳时间更改为该时区的时间。

原因:显示用户首选时区设置的时间,即使他们当前位于另一个时区的国家/地区。

有谁知道如何正确显示指定时区的时间戳?

【问题讨论】:

    标签: angularjs angularjs-filter


    【解决方案1】:

    了解 Javascript 的重要一点是,所有对时区的引用都是指系统上正在执行代码的时区,您无法控制该时区。您甚至不能相信客户端机器设置了正确的时区。因此,当您选择显示时区的选项时,您所能做的就是为您提供客户的时区。

    JavaScript 中的时区可能会变得复杂,这里有一个 blog post,其中包含了相当多的细节并提供了解决方案。

    处理时区的一种简单方法是将我的所有日​​期存储为 UTC,然后在需要显示它们时使用 moment.JS 库对其进行格式化。假设您的所有时间都存储在 UTC 中,您可以使用我在this plunker 中编写的过滤器来格式化您的日期并将它们操作到用户的首选时区。这里只是示例过滤器代码:

    // filter to set the timezone, assumes incoming time is in UTC
    angular
      .module('plunker')
      .filter('toUserTimezone', function() {
        return function(input, format, offset) {
    
          var output, timezoneText, inputCopy;
    
          // we need to copy the object so we don't cause any side-effects
          inputCopy = angular.copy(input);
    
          // check to make sure a moment object was passed
          if (!moment.isMoment(inputCopy)) {
            // do nothing
            return input;
    
          } else {
            // set default offset change to 0
            offset = offset || 0;
    
            // change the time by the offet  
            inputCopy.add(offset, 'hours');
    
            // this will need to be improved so the added text is in the format +hh:mm
            offset >= 0 ? timezoneText = '+' + offset : timezoneText = offset;
    
            // format the output to the requested format and add the timezone 
            output = inputCopy.format(format) + ' ' + timezoneText;
    
            return output;
          }
        };
      });
    

    moment 库非常好,每当我需要处理日期时,我都会将其包含在内,因为它很小。它还有一些非常强大的时区工具。您可以使用时区工具扩展上面的过滤器,使其适用于 DST 和偏移量不完全为一小时的时区,例如印度。

    更新: 在查看了时刻时区库之后,我们实际上可以简化过滤器代码。第一个解决方案更像是一个 hack,这个解决方案更加健壮,因为我们将保留原始时区数据。此外,我已将格式和时区转换分解为两个单独的过滤器。您可以在this plunker 中查看演示。

    这是一个转换时区的过滤器:

    angular
      .module('plunker')
      .filter('convertTimezone', function() {
        return function(input, timezone) {
          var output;
    
          // use clone to prevent side-effects
          output = input.clone().tz(timezone);
    
          // if the timezone was not valid, moment will not do anything, you may
          // want this to log if there was an issue
          if (moment.isMoment(output)) {
            return output;
          } else {
            // log error...    
            return input;
          }
        };
      });
    

    时区库允许您将字符串传递给 moment.tz() 方法,如果知道该字符串,则将进行转换,否则将不进行任何更改。 clone() 方法是防止副作用的更好方法,然后像我以前一样使用 angular.copy。

    现在这里是新的格式过滤器,与之前类似:

    angular
      .module('plunker')
      .filter('formatTime', function() {
        return function(input, format) {
    
          // check to make sure a moment object was passed
          if (!moment.isMoment(input)) {
            // do nothing
            return input;
          } else {
            return input.format(format);
          }
        };
      });
    

    综上所述,moment时区库还是蛮有用的!

    【讨论】:

    • 谢谢,这看起来很棒!您是否说也可以在偏移量中传递“Europe/Dublin”,然后以与选择时区相同的方式计算该值的偏移量?
    • 答案是肯定的,如果你使用 moment.JS 时区库是可能的。有关新过滤器和新演示,请参阅我的更新答案。
    • 谢谢!看起来真的很棒!
    【解决方案2】:

    根据 Angular 文档:

    时区

    Angular 日期时间过滤器使用 浏览器。同一个应用程序会显示不同的时间信息 取决于计算机的时区设置 应用程序正在运行。目前既不是 JavaScript 也不是 Angular 支持使用指定的时区显示日期 开发者。

    你看过momentjs和/或angular-moment吗?

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多