/** * timeago is a jquery plugin that makes it easy to support automatically * updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago"). * * @name timeago * @version 0.11.3 * @requires jquery v1.2.3+ * @author ryan mcgeary * @license mit license - http://www.opensource.org/licenses/mit-license.php * * for usage and examples, visit: * http://timeago.yarp.com/ * * copyright (c) 2008-2012, ryan mcgeary (ryan -[at]- mcgeary [*dot*] org) */ (function($) { $.timeago = function(timestamp) { if (timestamp instanceof date) { return inwords(timestamp); } else if (typeof timestamp === "string") { return inwords($.timeago.parse(timestamp)); } else if (typeof timestamp === "number") { return inwords(new date(timestamp)); } else { return inwords($.timeago.datetime(timestamp)); } }; var $t = $.timeago; $.extend($.timeago, { settings: { refreshmillis: 60000, allowfuture: false, strings: { prefixago: null, prefixfromnow: null, // 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", suffixago: "前", suffixfromnow: "从现在", seconds: "1分钟前", minute: "约1分钟", minutes: "%d分钟", hour: "约1小时", hours: "约 %d小时", day: "1天", days: "%d天", month: "约1个月", months: "%d个月", year: "约1年", years: "%d 年", wordseparator: "", 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; } } var seconds = math.abs(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 < 42 && substitute($l.day, 1) || days < 30 && substitute($l.days, math.round(days)) || days < 45 && substitute($l.month, 1) || days < 365 && substitute($l.months, math.round(days / 30)) || years < 1.5 && substitute($l.year, 1) || substitute($l.years, math.round(years)); var separator = $l.wordseparator === undefined ? " " : $l.wordseparator; return $.trim([prefix, words, suffix].join(separator)); }, 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) { var iso8601 = $t.istime(elem) ? $(elem).attr("datetime") : $(elem).attr("title"); return $t.parse(iso8601); }, istime: function(elem) { // jquery's `is()` doesn't play well with html5 in ie return $(elem).get(0).tagname.tolowercase() === "time"; // $(elem).is("time"); } }); $.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 && !($t.istime(element) && element.attr("title"))) { 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));