开发者

Timeago + DateJS = NaN

开发者 https://www.devze.com 2023-01-04 01:08 出处:网络
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):

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 circumventi开发者_运维百科ng this problem.

Thank you.

Brian


(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 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);
+      }
     }
   });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号