Taking what Bill Barry wrote - I then cleaned up the function a bit, got rid of a few declarations and returns, added error checking for future dates and a couple of test cases. Hope this is something along the lines of what you were looking for.
function timeAgo(dateString) {
/* removed rightNow, replaced with function call in seconds declaration */
/* removed seconds, minutes, hours, days declarations, declared below on set */
/* added new returnVal variable to remove all returns below with 1 return */
var returnVal, then = new Date(dateString);
/* left alone */
if($.browser.msie) then = Date.parse(dateString.replace(/( \+)/, ' UTC$1'));
/* replaced rightNow in seconds declare with new Date() */
/* declared variables in place instead of above */
var seconds = (new Date() - then) / 1000;
var minutes = seconds / 60;
var hours = minutes / 60;
var days = hours * 24;
/* Check for negative value of seconds (negative indicates a future date) */
if(seconds < 0) return "";
/* replaced if statements with ternary operations - might not save much performance wise, but looks cleaner */
seconds < 2 ? returnVal = {tAgo: "right now", timeCheck: false} :
seconds < 60 ? returnVal = {tAgo: Math.floor(seconds) + " seconds ago", timeCheck: false} :
returnVal = "";
minutes < 2 ? returnVal = {tAgo: "about 1 minute ago", timeCheck: false} :
minutes < 60 ? returnVal = {tAgo: Math.floor(minutes) + " minutes ago", timeCheck: false} :
returnVal = {tAgo: "over a year ago", timeCheck: true};
hours < 2 ? returnVal = {tAgo: "about 1 hour ago", timeCheck: false} :
hours < 24 ? returnVal = {tAgo: Math.floor(hours) + " hours ago", timeCheck: true} :
returnVal = {tAgo: "over a year ago", timeCheck: true};
days < 2 ? returnVal = {tAgo: "yesterday", timeCheck: true} :
days < 365 ? returnVal = {tAgo: Math.floor(days) + " days ago", timeCheck: true} :
returnVal = {tAgo: "over a year ago", timeCheck: true};
/* Remove JSON.stringify() if you want to return an object */
return JSON.stringify(returnVal);
}
function testTimeAgo(){
document.write("For ~Tue Apr 07 22:52:51 +0000 2009~:: " + timeAgo("Tue Apr 07 22:52:51 +0000 2009") + "<br>");
document.write("For ~Tue July 16 22:52:51 +0000 2012~:: " + timeAgo("Tue July 16 22:52:51 +0000 2012") + "<br>");
document.write("For ~Tue Apr 07 22:52:51 +0000 2013~:: " + timeAgo("Tue Apr 07 22:52:51 +0000 2013") + "<br>");
document.write("For ~Tue June 07 22:52:51 +0000 2012~:: " + timeAgo("Tue June 07 22:52:51 +0000 2012") + "<br>");
}
/* Run a few tests */
testTimeAgo();
Without comments or test case, and a couple more improvements:
function timeAgo(dateString) {
var returnVal, then;
$.browser.msie ? then = Date.parse(dateString.replace(/( \+)/, ' UTC$1')) : then = new Date(dateString);
var seconds = new Date() - then) / 1000;
var minutes = seconds / 60;
var hours = minutes / 60;
var days = hours * 24;
if(seconds < 0) return "";
seconds < 2 ? returnVal = {tAgo: "right now", timeCheck: false} :
seconds < 60 ? returnVal = {tAgo: Math.floor(seconds) + " seconds ago", timeCheck: false} :
returnVal = "";
minutes < 2 ? returnVal = {tAgo: "about 1 minute ago", timeCheck: false} :
minutes < 60 ? returnVal = {tAgo: Math.floor(minutes) + " minutes ago", timeCheck: false} :
returnVal = {tAgo: "over a year ago", timeCheck: true};
hours < 2 ? returnVal = {tAgo: "about 1 hour ago", timeCheck: false} :
hours < 24 ? returnVal = {tAgo: Math.floor(hours) + " hours ago", timeCheck: true} :
returnVal = {tAgo: "over a year ago", timeCheck: true};
days < 2 ? returnVal = {tAgo: "yesterday", timeCheck: true} :
days < 365 ? returnVal = {tAgo: Math.floor(days) + " days ago", timeCheck: true} :
returnVal = {tAgo: "over a year ago", timeCheck: true};
return returnVal;
}
$.browseris deprecated - is there someway to do that with bug-detection? – Inkbug Jul 18 '12 at 13:38