时间过去 +日期JS = NaN

发布于 2024-09-06 03:49:41 字数 743 浏览 6 评论 0原文

我正在尝试使用 timeago (来源),与 datejs,它不起作用。这是我希望能够工作的一些示例代码(假设已加载 timeagodatejs):

>>> d = new Date()
Mon Jun 21 2010 13:24:37 GMT-0400 (EST) { _orient=1, more...}
>>> d.toISOString()          // datejs.toISOString
"2010-06-21T17:24:37.501Z"   // this is a valid ISO8601 string, I believe
>>> $.timeago(d.toISOString()) // this should work
"NaN years ago"

我非常感谢任何关于为什么这可能会失败的输入,以及如何解决或规避这一问题。

谢谢。

布莱恩

I'm trying to use timeago (source), with datejs, and it's not working. Here's some sample code I'd expect to work (given that timeago and datejs are loaded):

>>> d = new Date()
Mon Jun 21 2010 13:24:37 GMT-0400 (EST) { _orient=1, more...}
>>> d.toISOString()          // datejs.toISOString
"2010-06-21T17:24:37.501Z"   // this is a valid ISO8601 string, I believe
>>> $.timeago(d.toISOString()) // this should work
"NaN years ago"

I'd be much obliged for any input as to why this may be failing, and how one could go about fixing or circumventing this problem.

Thank you.

Brian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

机场等船 2024-09-13 03:49:41

(我是Timeago的作者)

问题在于datejs输出的ISO8601时间戳包含一个毫秒值。 Timeago 目前不支持这种详细的 ISO8601 时间戳;它仅支持 ISO8601 规范的一个子集。

这不是我第一次听说这个问题;是时候推出一个补丁来处理 millis 了。我创建了一个问题来跟踪此问题。请留意即将推出的 Timeago 版本。可能是 v0.9。

更新: 现在有一个新版本的 Timeago (v0.9),支持时间戳中的毫秒。 在此处下载。这是相关提交

(I'm the author of Timeago)

The problem lies in the fact that the ISO8601 timestamp output by datejs includes a milliseconds value. Timeago currently doesn't support this detailed of an ISO8601 timestamp; it only supports a subset of the ISO8601 spec.

This isn't the first time I've heard about this problem; it's time there's a patch to handle the millis. I created an issue to track this. Look out for an upcoming version of Timeago. Likely v0.9.

Update: There's now a new version of Timeago (v0.9) that supports milliseconds in the timestamps. Download it here. Here's the relevant commit.

第七度阳光i 2024-09-13 03:49:41

我对 jquery.timeago.js 进行了以下修补程序,解决了该问题:

diff -r 89cc78838c70 media/js/contrib/jquery.timeago.js
--- a/media/js/contrib/jquery.timeago.js    Mon Jun 21 10:22:12 2010 -0400
+++ b/media/js/contrib/jquery.timeago.js    Mon Jun 21 13:45:32 2010 -0400
@@ -87,8 +87,14 @@
     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);
+      var date_string = isTime ? $(elem).attr("datetime") : $(elem).attr("title");
+
+        // for use with datejs @ http://www.datejs.com/
+      if (typeof(Date.parse) == 'function') {
+        return Date.parse(date_string);
+      } else {
+        return $t.parse(date_string);
+      }
     }
   });

I made the following patch to jquery.timeago.js, which resolves the problem:

diff -r 89cc78838c70 media/js/contrib/jquery.timeago.js
--- a/media/js/contrib/jquery.timeago.js    Mon Jun 21 10:22:12 2010 -0400
+++ b/media/js/contrib/jquery.timeago.js    Mon Jun 21 13:45:32 2010 -0400
@@ -87,8 +87,14 @@
     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);
+      var date_string = isTime ? $(elem).attr("datetime") : $(elem).attr("title");
+
+        // for use with datejs @ http://www.datejs.com/
+      if (typeof(Date.parse) == 'function') {
+        return Date.parse(date_string);
+      } else {
+        return $t.parse(date_string);
+      }
     }
   });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文