Skip to content

Added limit for days in the future to convert #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 21 additions & 43 deletions jquery.timeago.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
*
* @name timeago
* @version 1.1.0
* @version 1.0.1
* @requires jQuery v1.2.3+
* @author Ryan McGeary
* @license MIT License - http://www.opensource.org/licenses/mit-license.php
Expand All @@ -13,16 +13,7 @@
*
* Copyright (c) 2008-2013, Ryan McGeary (ryan -[at]- mcgeary [*dot*] org)
*/

(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
(function($) {
$.timeago = function(timestamp) {
if (timestamp instanceof Date) {
return inWords(timestamp);
Expand All @@ -40,7 +31,7 @@
settings: {
refreshMillis: 60000,
allowFuture: false,
localeTitle: false,
daysInFuture: 0,
strings: {
prefixAgo: null,
prefixFromNow: null,
Expand All @@ -67,6 +58,14 @@
var suffix = $l.suffixAgo;
if (this.settings.allowFuture) {
if (distanceMillis < 0) {
// if more than 3 days out, show the due date.
if (this.settings.daysInFuture > 0 && distanceMillis < -(1000*60*60*24*this.settings.daysInFuture)) {
var d_names = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var d = new Date();
d.setTime(d.getTime()+Math.abs(distanceMillis));
return d_names[d.getDay()]+", "+m_names[d.getMonth()]+" "+d.getDate()+", "+d.getFullYear();
}
prefix = $l.prefixFromNow;
suffix = $l.suffixFromNow;
}
Expand Down Expand Up @@ -97,7 +96,7 @@
substitute($l.years, Math.round(years));

var separator = $l.wordSeparator || "";
if ($l.wordSeparator === undefined) { separator = " "; }
if ($l.wordSeparator === undefined) { separator = " "; }
return $.trim([prefix, words, suffix].join(separator));
},
parse: function(iso8601) {
Expand All @@ -118,34 +117,15 @@
}
});

// functions that can be called via $(el).timeago('action')
// init is default when no action is given
// functions are called with context of a single element
var functions = {
init: function(){
var refresh_el = $.proxy(refresh, this);
refresh_el();
var $s = $t.settings;
if ($s.refreshMillis > 0) {
setInterval(refresh_el, $s.refreshMillis);
}
},
update: function(time){
$(this).data('timeago', { datetime: $t.parse(time) });
refresh.apply(this);
}
};
$.fn.timeago = function() {
var self = this;
self.each(refresh);

$.fn.timeago = function(action, options) {
var fn = action ? functions[action] : functions.init;
if(!fn){
throw new Error("Unknown function name '"+ action +"' for timeago");
var $s = $t.settings;
if ($s.refreshMillis > 0) {
setInterval(function() { self.each(refresh); }, $s.refreshMillis);
}
// each over objects here and call the requested function
this.each(function(){
fn.call(this, options);
});
return this;
return self;
};

function refresh() {
Expand All @@ -161,9 +141,7 @@
if (!element.data("timeago")) {
element.data("timeago", { datetime: $t.datetime(element) });
var text = $.trim(element.text());
if ($t.settings.localeTitle) {
element.attr("title", element.data('timeago').datetime.toLocaleString());
} else if (text.length > 0 && !($t.isTime(element) && element.attr("title"))) {
if (text.length > 0 && !($t.isTime(element) && element.attr("title"))) {
element.attr("title", text);
}
}
Expand All @@ -181,4 +159,4 @@
// fix for IE6 suckage
document.createElement("abbr");
document.createElement("time");
}));
}(jQuery));