Skip to content

Commit

Permalink
Added dateOnly option (which uses today/tomorrow/yesterday and is mid…
Browse files Browse the repository at this point in the history
…night-aware)
  • Loading branch information
cameron314 committed Dec 31, 2011
1 parent ee56583 commit 9a2657f
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions jquery.timeago.js
Expand Up @@ -29,6 +29,7 @@
settings: {
refreshMillis: 60000,
allowFuture: false,
dateOnly: false,
strings: {
prefixAgo: null,
prefixFromNow: null,
Expand All @@ -45,10 +46,13 @@
months: "%d months",
year: "about a year",
years: "%d years",
today: "today",
tomorrow: "tomorrow",
yesterday: "yesterday",
numbers: []
}
},
inWords: function(distanceMillis) {
inWords: function(distanceMillis, originalDate) {
var $l = this.settings.strings;
var prefix = $l.prefixAgo;
var suffix = $l.suffixAgo;
Expand All @@ -64,8 +68,33 @@
var hours = minutes / 60;
var days = hours / 24;
var years = days / 365;



if (this.settings.dateOnly) {
// Day difference code from https://github.com/brianmhunt/jquery-timeago/commit/dfbef678758da942127fc60a10638f12dd2c3ee2#commitcomment-97699
var dateWithoutTime = new Date(
originalDate.getFullYear(),
originalDate.getMonth(),
originalDate.getDate()
);

var daysDifference = Math.floor((new Date() - dateWithoutTime) / (1000 * 60 * 60 * 24));

if (Math.abs(daysDifference) <= 1) {
var word =
daysDifference == -1 && $l.tomorrow ||
daysDifference == 0 && $l.today ||
$l.yesterday;
return $.trim(word);
}

// Not today, tomorrow, or yesterday; subsequent code will choose words, so make
// sure that correct day difference (knowing about midnight) is used (since it's more
// than one day's difference, hours should be >= 48 even if the actual span is less)
hours = 48;
days = Math.abs(daysDifference);
}


function substitute(stringOrFunction, number) {
var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction;
var value = ($l.numbers && $l.numbers[number]) || number;
Expand All @@ -91,7 +120,7 @@
s = s.replace(/\.\d\d\d+/,""); // remove milliseconds
s = s.replace(/-/,"/").replace(/-/,"/");
s = s.replace(/T/," ").replace(/Z/," UTC");
s = s.replace(/([\+\-]\d\d)$/,"$1:00"); // -04 -> -04:00
s = s.replace(/([\+\-]\d\d)$/,"$1:00"); // -04 -> -04:00
s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
return new Date(s);
},
Expand Down Expand Up @@ -135,7 +164,7 @@
}

function inWords(date) {
return $t.inWords(distance(date));
return $t.inWords(distance(date), date);
}

function distance(date) {
Expand Down

0 comments on commit 9a2657f

Please sign in to comment.