PageSourceSearch

https://www.enjore.com/js_v5122/jquery-timeago/jquery.timeago.js

js enjore.com collected 2026-09-24 10:18:53 UTC 4,607 bytes, 148 lines download raw bytes

vendor: 4,607 bytes, lines 1-148
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.1
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-2011, Ryan McGeary (ryanonjavascript -[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 {
23      return inWords($.timeago.datetime(timestamp));
24    }
25  };
26  var $t = $.timeago;
27
28  $.extend($.timeago, {
29    settings: {
30      refreshMillis: 60000,
31      allowFuture: false,
32      strings: {
33        prefixAgo: null,
34        prefixFromNow: null,
35        suffixAgo: "ago",
36        suffixFromNow: "from now",
37        seconds: "less than a minute",
38        minute: "about a minute",
39        minutes: "%d minutes",
40        hour: "about an hour",
41        hours: "about %d hours",
42        day: "a day",
43        days: "%d days",
44        month: "about a month",
45        months: "%d months",
46        year: "about a year",
47        years: "%d years",
48        wordSeparator: " ",
49        numbers: []
50      }
51    },
52    inWords: function(distanceMillis) {
53      var $l = this.settings.strings;
54      var prefix = $l.prefixAgo;
55      var suffix = $l.suffixAgo;
56      if (this.settings.allowFuture) {
57        if (distanceMillis < 0) {
58          prefix = $l.prefixFromNow;
59          suffix = $l.suffixFromNow;
60        }
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
69      function substitute(stringOrFunction, number) {
70        var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction;
71        var value = ($l.numbers && $l.numbers[number]) || number;
72        return string.replace(/%d/i, value);
73      }
74
75      var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
76        seconds < 90 && substitute($l.minute, 1) ||
77        minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
78        minutes < 90 && substitute($l.hour, 1) ||
79        hours < 24 && substitute($l.hours, Math.round(hours)) ||
80        hours < 42 && substitute($l.day, 1) ||
81        days < 30 && substitute($l.days, Math.round(days)) ||
82        days < 45 && substitute($l.month, 1) ||
83        days < 365 && substitute($l.months, Math.round(days / 30)) ||
84        years < 1.5 && substitute($l.year, 1) ||
85        substitute($l.years, Math.round(years));
86
87      var separator = $l.wordSeparator === undefined ?  " " : $l.wordSeparator;
88      return $.trim([prefix, words, suffix].join(separator));
89    },
90    parse: function(iso8601) {
91      var s = $.trim(iso8601);
92      s = s.replace(/\.\d\d\d+/,""); // remove milliseconds
93      s = s.replace(/-/,"/").replace(/-/,"/");
94      s = s.replace(/T/," ").replace(/Z/," UTC");
95      s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
96      return new Date(s);
97    },
98    datetime: function(elem) {
99      // jQuery's `is()` doesn't play well with HTML5 in IE
100      var isTime = $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time");
101      var iso8601 = isTime ? $(elem).attr("datetime") : $(elem).attr("title");
102      return $t.parse(iso8601);
103    }
104  });
105
106  $.fn.timeago = function() {
107    var self = this;
108    self.each(refresh);
109
110    var $s = $t.settings;
111    if ($s.refreshMillis > 0) {
112      setInterval(function() { self.each(refresh); }, $s.refreshMillis);
113    }
114    return self;
115  };
116
117  function refresh() {
118    var data = prepareData(this);
119    if (!isNaN(data.datetime)) {
120      $(this).text(inWords(data.datetime));
121    }
122    return this;
123  }
124
125  function prepareData(element) {
126    element = $(element);
127    if (!element.data("timeago")) {
128      element.data("timeago", { datetime: $t.datetime(element) });
129      var text = $.trim(element.text());
130      if (text.length > 0) {
131        element.attr("title", text);
132      }
133    }
134    return element.data("timeago");
135  }
136
137  function inWords(date) {
138    return $t.inWords(distance(date));
139  }
140
141  function distance(date) {
142    return (new Date().getTime() - date.getTime());
143  }
144
145  // fix for IE6 suckage
146  document.createElement("abbr");
147  document.createElement("time");
148}(jQuery));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.