如何更改此jquery插件的时区/时间戳? [英] How do I change the timezone/timestamp of this jquery plugin?

查看:103
本文介绍了如何更改此jquery插件的时区/时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个名为timeago的插件:timeago.yarp.com

I'm using this plugin called timeago found here: timeago.yarp.com

它运行良好,除了它在看似不同的时区运行。我住在美国东部(费城时区),当我把确切时间EST放入timeago插件时(比如2011-05-28,13:47:18),它在我的html页面上重印了四个小时。当我写2011-05-28,17:47:18(比我住的地方晚4个小时),那么它重印为不到一分钟之前

It works great, except that it operates in what appears to be a different timezone. I live in Eastern US (Philadelphia timezone) and when I put the exact time EST into the timeago plugin (say 2011-05-28, 13:47:18), it reprints as four hours later on my html page. When I write 2011-05-28, 17:47:18 (four hours later than my actual time from where I live), THEN it reprints as "less than a minute ago"

这是jquery插件代码:

Here's the jquery plugin code:

        (function($) {
      $.timeago = function(timestamp) {
    if (timestamp instanceof Date) {
      return inWords(timestamp);
    } else if (typeof timestamp === "string") {
      return inWords($.timeago.parse(timestamp));
    } else {
      return inWords($.timeago.datetime(timestamp));
    }
    };
    var $t = $.timeago;

    $.extend($.timeago, {
    settings: {
      refreshMillis: 60000,
      allowFuture: false,
      strings: {
        prefixAgo: "added",
        prefixFromNow: "added",
        suffixAgo: "ago",
        suffixFromNow: "from now",
        seconds: "less than a minute",
        minute: "about a minute",
        minutes: "%d minutes",
        hour: "about an hour",
        hours: "about %d hours",
        day: "a day",
        days: "%d days",
        month: "about a month",
        months: "%d months",
        year: "about a year",
        years: "%d years",
        numbers: []
      }
    },
    inWords: function(distanceMillis) {
      var $l = this.settings.strings;
      var prefix = $l.prefixAgo;
      var suffix = $l.suffixAgo;
      if (this.settings.allowFuture) {
        if (distanceMillis < 0) {
          prefix = $l.prefixFromNow;
          suffix = $l.suffixFromNow;
        }
        distanceMillis = Math.abs(distanceMillis);
      }

      var seconds = distanceMillis / 1000;
      var minutes = seconds / 60;
      var hours = minutes / 60;
      var days = hours / 24;
      var years = days / 365;

      function substitute(stringOrFunction, number) {
        var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction;
        var value = ($l.numbers && $l.numbers[number]) || number;
        return string.replace(/%d/i, value);
      }

      var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
        seconds < 90 && substitute($l.minute, 1) ||
        minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
        minutes < 90 && substitute($l.hour, 1) ||
        hours < 24 && substitute($l.hours, Math.round(hours)) ||
        hours < 48 && substitute($l.day, 1) ||
        days < 30 && substitute($l.days, Math.floor(days)) ||
        days < 60 && substitute($l.month, 1) ||
        days < 365 && substitute($l.months, Math.floor(days / 30)) ||
        years < 2 && substitute($l.year, 1) ||
        substitute($l.years, Math.floor(years));

      return $.trim([prefix, words, suffix].join(" "));
    },
    parse: function(iso8601) {
      var s = $.trim(iso8601);
      s = s.replace(/\.\d\d\d+/,""); // remove milliseconds
      s = s.replace(/-/,"/").replace(/-/,"/");
      s = s.replace(/T/," ").replace(/Z/," UTC");
      s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
      return new Date(s);
    },
    datetime: function(elem) {
      // jQuery's `is()` doesn't play well with HTML5 in IE
      var isTime = $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time");
      var iso8601 = isTime ? $(elem).attr("datetime") : $(elem).attr("title");
      return $t.parse(iso8601);
    }
    });
  $.fn.timeago = function() {
    var self = this;
    self.each(refresh);

    var $s = $t.settings;
    if ($s.refreshMillis > 0) {
      setInterval(function() { self.each(refresh); }, $s.refreshMillis);
    }
    return self;
   };

  function refresh() {
    var data = prepareData(this);
    if (!isNaN(data.datetime)) {
      $(this).text(inWords(data.datetime));
    }
    return this;
  }

  function prepareData(element) {
    element = $(element);
    if (!element.data("timeago")) {
      element.data("timeago", { datetime: $t.datetime(element) });
      var text = $.trim(element.text());
      if (text.length > 0) {
        element.attr("title", text);
      }
    }
     return element.data("timeago");
   }

    function inWords(date) {
    return $t.inWords(distance(date));
    }

    function distance(date) {
      return (new Date().getTime() - date.getTime());
    }

    // fix for IE6 suckage
    document.createElement("abbr");
    document.createElement("time");
    }(jQuery));

我意识到这个问题非常小,如果我只记得插件就可以轻松修复工作延迟4小时,但我仍然想知道答案是否可以提供。

I realize this problem is something very minor and can be easily fixed if I were to just remember the plugin works on a 4 hour delay, but I'd still like to know the answer if possible to provide.

谢谢!

推荐答案

已更新

我要试一试但是我必须添加免责声明,我自己有点模糊,所以我可能错了!

插件需要时间在 ISO 8601格式,其中还可以包含偏移信息。我现在尝试使用插件,这就是我所看到的(在 14:42 EDT ,距离测试时间字符串大约3分钟):

The plugin expects the time in the ISO 8601 format which can also include offset information. I've now tried using the plugin and this is what I see (at 14:42 EDT, about 3 minutes from the test time string):

  • When a trailing Z is used, indicating Zulu time or an offset of 0 from UTC, the plugin interprets it as UTC (obviously) and when printing the relative time string, it takes into consideration your actual timezone. This causes the extra 4 hours to be added (EST is UTC-5 (UTC-4 when following DST, like now)).

2011-05-28T14:39:33Z 打印as 大约4小时前

当尾随 Z 使用,该插件根据您的时区解释指定的时间,它似乎工作正常(只要时间戳的时区和您正在查看的时区这个时间戳是相同的)。这与维基百科文章所说的一致:

When a trailing Z is not used, the plugin interprets the time specified according to your timezone and it seems to work just fine (as long as the timezone for the timestamp and the timezone you're viewing this timestamp in are the same). This is in line with what the Wikipedia article has to say:


如果没有给出时间表示的UTC关系信息,则假定时间是当地时间。虽然在同一时区进行通信时假定当地时间可能是安全的,但是当用于跨不同时区的通信时,它是不明确的。通常最好使用标准符号表示时区(区域指示符)。

If no UTC relation information is given with a time representation, the time is assumed to be in local time. While it may be safe to assume local time when communicating in the same time zone, it is ambiguous when used in communicating across different time zones. It is usually preferable to indicate a time zone (zone designator) using the standard’s notation.

这不是推荐的方式,因为它是因为时间戳将被解释为 时区不正确的时间戳,所以会把时间搞得一团糟。

This would not be a recommended way since it's going to mess up the times when viewed from elsewhere since the timestamp will be interpreted as being the timestamp for that timezone which is incorrect.

2011-05-28T14:39:33 打印为 3分钟前

当指定尾随 Z 以及时区偏移时(格式为 hh:mm ,只有 hh 似乎被忽略了),它似乎仍然可以正常工作。

When a trailing Z is specified along with the timezone offset (in the format of hh:mm, only hh seems to be ignored), it still seems to work just fine.

2011-05-28T14:39:33Z-04:00 打印为 3分钟前

你可以在这里看到一个有效的例子: http://jsfiddle.net/nogoodatcoding/SVgck/

You can see a working example here: http://jsfiddle.net/nogoodatcoding/SVgck/

你不应该是c挂起插件本身的时区/偏移,因为这将导致来自其他时区的访问者看到不正确的值。

You shouldn't be changing the timezone/offset of the plugin itself since that will cause visitors from other timezones to see incorrect values.


  • 一个修复方法是在日期时间字符串中指定时区偏移量: 2011-05-28, 13:47:18Z-04:00 - 从某种意义上说,它是一个更完整的时间描述,因为它还包括UTC偏移信息。

  • 取决于如何生成此页面(如果它不仅仅是静态HTML),另一个选项是修复服务器端代码,使其输出的日期时间字符串采用UTC格式 - 如果没有内置,则应该是能够找到从本地时区的时间戳转换为UTC的库。这就是我见过的网站所做的事情 - 例如,StackOverflow(或Twitter和Facebook)上的时间戳是UTC时间 - 然后根据用户的时区对它们进行不同的格式化。

  • One fix is to also specify the timezone offset in your date-time strings: 2011-05-28, 13:47:18Z-04:00 - in a sense, a more complete description of the time since it also includes the UTC offset information.
  • Depending on how you're generating this page (if it's not just static HTML), the other option is to fix your server-side code so that the date-time string it outputs is in the UTC format - if not built in, you should be able to find a library that does the conversion from a timestamp in your local timezone into UTC. This is how sites I've seen do it - for example, the timestamps here on StackOverflow (or on Twitter and Facebook) are in UTC time - they are then formatted differently based on the user's timezone.

这篇关于如何更改此jquery插件的时区/时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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