jquery.timeago.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Timeago is a jQuery plugin that makes it easy to support automatically
  3. * updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
  4. *
  5. * @name timeago
  6. * @version 0.11.4
  7. * @requires jQuery v1.2.3+
  8. * @author Ryan McGeary
  9. * @license MIT License - http://www.opensource.org/licenses/mit-license.php
  10. *
  11. * For usage and examples, visit:
  12. * http://timeago.yarp.com/
  13. *
  14. * Copyright (c) 2008-2012, Ryan McGeary (ryan -[at]- mcgeary [*dot*] org)
  15. */
  16. (function($) {
  17. $.timeago = function(timestamp) {
  18. if (timestamp instanceof Date) {
  19. return inWords(timestamp);
  20. } else if (typeof timestamp === "string") {
  21. return inWords($.timeago.parse(timestamp));
  22. } else if (typeof timestamp === "number") {
  23. return inWords(new Date(timestamp));
  24. } else {
  25. return inWords($.timeago.datetime(timestamp));
  26. }
  27. };
  28. var $t = $.timeago;
  29. $.extend($.timeago, {
  30. settings: {
  31. refreshMillis: 60000,
  32. allowFuture: false,
  33. strings: {
  34. prefixAgo: null,
  35. prefixFromNow: null,
  36. suffixAgo: "ago",
  37. suffixFromNow: "from now",
  38. seconds: "less than a minute",
  39. minute: "about a minute",
  40. minutes: "%d minutes",
  41. hour: "about an hour",
  42. hours: "about %d hours",
  43. day: "a day",
  44. days: "%d days",
  45. month: "about a month",
  46. months: "%d months",
  47. year: "about a year",
  48. years: "%d years",
  49. wordSeparator: " ",
  50. numbers: []
  51. }
  52. },
  53. inWords: function(distanceMillis) {
  54. var $l = this.settings.strings;
  55. var prefix = $l.prefixAgo;
  56. var suffix = $l.suffixAgo;
  57. if (this.settings.allowFuture) {
  58. if (distanceMillis < 0) {
  59. prefix = $l.prefixFromNow;
  60. suffix = $l.suffixFromNow;
  61. }
  62. }
  63. var seconds = Math.abs(distanceMillis) / 1000;
  64. var minutes = seconds / 60;
  65. var hours = minutes / 60;
  66. var days = hours / 24;
  67. var years = days / 365;
  68. function substitute(stringOrFunction, number) {
  69. var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction;
  70. var value = ($l.numbers && $l.numbers[number]) || number;
  71. return string.replace(/%d/i, value);
  72. }
  73. var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
  74. seconds < 90 && substitute($l.minute, 1) ||
  75. minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
  76. minutes < 90 && substitute($l.hour, 1) ||
  77. hours < 24 && substitute($l.hours, Math.round(hours)) ||
  78. hours < 42 && substitute($l.day, 1) ||
  79. days < 30 && substitute($l.days, Math.round(days)) ||
  80. days < 45 && substitute($l.month, 1) ||
  81. days < 365 && substitute($l.months, Math.round(days / 30)) ||
  82. years < 1.5 && substitute($l.year, 1) ||
  83. substitute($l.years, Math.round(years));
  84. var separator = $l.wordSeparator === undefined ? " " : $l.wordSeparator;
  85. return $.trim([prefix, words, suffix].join(separator));
  86. },
  87. parse: function(iso8601) {
  88. var s = $.trim(iso8601);
  89. s = s.replace(/\.\d+/,""); // remove milliseconds
  90. s = s.replace(/-/,"/").replace(/-/,"/");
  91. s = s.replace(/T/," ").replace(/Z/," UTC");
  92. s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
  93. return new Date(s);
  94. },
  95. datetime: function(elem) {
  96. var iso8601 = $t.isTime(elem) ? $(elem).attr("datetime") : $(elem).attr("title");
  97. return $t.parse(iso8601);
  98. },
  99. isTime: function(elem) {
  100. // jQuery's `is()` doesn't play well with HTML5 in IE
  101. return $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time");
  102. }
  103. });
  104. $.fn.timeago = function() {
  105. var self = this;
  106. self.each(refresh);
  107. var $s = $t.settings;
  108. if ($s.refreshMillis > 0) {
  109. setInterval(function() { self.each(refresh); }, $s.refreshMillis);
  110. }
  111. return self;
  112. };
  113. function refresh() {
  114. var data = prepareData(this);
  115. if (!isNaN(data.datetime)) {
  116. $(this).text(inWords(data.datetime));
  117. }
  118. return this;
  119. }
  120. function prepareData(element) {
  121. element = $(element);
  122. if (!element.data("timeago")) {
  123. element.data("timeago", { datetime: $t.datetime(element) });
  124. var text = $.trim(element.text());
  125. if (text.length > 0 && !($t.isTime(element) && element.attr("title"))) {
  126. element.attr("title", text);
  127. }
  128. }
  129. return element.data("timeago");
  130. }
  131. function inWords(date) {
  132. return $t.inWords(distance(date));
  133. }
  134. function distance(date) {
  135. return (new Date().getTime() - date.getTime());
  136. }
  137. // fix for IE6 suckage
  138. document.createElement("abbr");
  139. document.createElement("time");
  140. }(jQuery));