jQuery.timeago 插件的时区问题
我在我的博客中使用 timeago jQuery 插件,但计时本身似乎有问题,我无法指出问题的原因是什么。
目前此处当地时间为 2011 年 5 月 31 日 02:30 (GMT+DST)。现在我使用的示例日期是...2011 年 5 月 31 日 02:01。下面的标签是
<abbr class="timeago" title="2011-05-31T02:01:44+00:00">May 31st, 2011</abbr>
Yet the jQueryoutputs that the date is "32 minutes from now", 由于某种原因提前了一小时。
有人知道我在这里做错了什么吗?
I am using timeago jQuery plugin for my blog, but there seems to be a problem with the timing itself, and I cant put my finger and what the cause of the problem is.
Its currently May 31st, 2011 02:30 local time here (GMT+DST). Now the example date I have used is... May 31st, 2011 02:01. The following tag for this would be
<abbr class="timeago" title="2011-05-31T02:01:44+00:00">May 31st, 2011</abbr>
Yet the jQuery outputs that the date is "32 minutes from now", Its one hour ahead for some reason.
Anyone know what I'm doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里冒险猜测,所以我可能是完全错误的。
问题是您指定的测试 timesatmp 的偏移量为
0
,因此它与 UTC 相同 - 但 < strong>这与您在 GMT 时区遵循夏令时的时间不同。GMT 与 UTC 相同,即偏移量为
0
。但是,您提到 DST,并且在遵循夏令时时,您的时钟会向前移动 1 小时,UTC 偏移量也会向前移动 1 小时。因此,您现在的时间(相对于 UTC)实际上是UTC + 0100
。这就是额外时间的来源。首先将所有时间转换为 UTC 可能会有所帮助,毕竟这就是它的用途,对吗?
02:30 GMT
而 DST 为01:30 UTC
。因此,与此相比,02:01 UTC
的测试时间戳大约是未来半小时 - 因此是从现在起 32 分钟
。如果您希望看到
30 分钟前
,那么您的测试日期应该是2011-05-31T01:01:44+00:00
以表示 UTC相当于时钟显示的02:01
时间的时间戳。最安全的解决方案是始终使用 UTC 时间戳 - 这就是 Facebook 和 Twitter 发布帖子的日期。然后可以针对每个用户的时区明确地解释 UTC 时间戳并对其进行格式化。
那么,您的工作就是确保您使用的时间戳不仅仅是您在钟面上看到的时间戳,而是它的 UTC 等效值 - 在您当前的时区中,UTC 时间可能比显示的时间晚 1 小时。根据您用来生成 HTML 的内容,您应该能够找到一个内置于语言/平台或作为第三方库的函数,该函数将为您提供与显示在考虑 DST 的您所在时区的时钟。
Hazarding a guess here so I might be quite wrong.
The problem is that the test timesatmp you've specified has an offset of
0
so it's the same as UTC - but that's not the same as your time when following DST in the GMT timezone.GMT is the same as UTC i.e. the offset is
0
. However, you mention DST and when following Daylight Savings Time, your clock moves forward by 1 hour and so does your UTC offset. So your time right now, with respect to UTC, is actuallyUTC + 0100
. That's where the extra hour is coming from.It might help to convert all times to UTC first, after all, that's what it's for, right?
02:30 GMT
while following DST is01:30 UTC
. So compared to this, the test timestamp of02:01 UTC
, is about a half hour in the future - hence the32 minutes from now
.If you were expecting to see
30 minutes ago
instead, your test date should have been2011-05-31T01:01:44+00:00
in order to express a UTC timestamp that was equivalent to the time of02:01
by your clock display.The safest solution would be to use UTC timestamps always - this is how SO, Facebook and Twitter date their posts. The UTC timestamp can then be unambiguously interpreted and formatted for each user's timezone.
Your job, then, would be to make sure the timestamp you use is not simply what you see on your clock face, but the UTC equivalent of it - in your current timezone, the UTC time could be 1 hour behind the time displayed. And depending on what you're using to generate your HTML, you should be able to find a function, built-into the language/platform or as a third-party library, that will give you the UTC equivalent of the current time displayed on your clocks in your timezone considering DST.
你不说DST是什么,我会假设它是+1h,与上面的no.good.at.coding相同
您现在的时间是 02:30,即 01:30 UTC。您的日期是从 02:01 UTC 开始的,即您的时间 03:01,也就是未来的半小时。
正如建议的那样,无论如何,您都应该将所有日期保存为 UTC。例如,Python 有一个 datetime.utcnow() 方法。始终保存为 UTC,然后在显示时转换为本地时区。
You don't say what the DST is, I will assume it's +1h, same as no.good.at.coding above
You are at 02:30 which is 01:30 UTC. Your date is from 02:01 UTC which is 03:01 your time, which then is half an hour in the future.
As suggested you should save all dates as UTC anyway. Python for example has a datetime.utcnow() method. Always save as UTC, then convert to the local timezone when displaying.