Shorten Time Return Function I have this function which returns the time that a Tweet was created based on the created_at value that comes when a timeline is pulled. I also check whether or not the tweet was created more than 2 hours ago, and if it was, have an action execute. I was wondering, is there a way that this code could be shortened down because it currently feels very long. I thought about using a switch statement instead of all the ifs, but it didn't really shorten the function down at all.


function timeAgo(dateString) {
   var rightNow = new Date();
   var then = new Date(dateString);
   if ($.browser.msie) {
      then = Date.parse(dateString.replace(/( \+)/, ' UTC$1'));
   }
   var diff = rightNow - then;
   var second = 1000;
   var minute = second * 60;
   var hour = minute * 60;
   var day = hour * 24;
   var week = day * 7;
   if (isNaN(diff) || diff &lt; 0) {
      return "";
   }
   if (diff &lt; second * 2) {
      return {tAgo: "right now", timeCheck: false};
   }
   if (diff &lt; minute) {
      return {tAgo: Math.floor(diff / second) + " seconds ago", timeCheck: false};
   }
   if (diff &lt; minute * 2) {
      return {tAgo: "about 1 minute ago", timeCheck: false};
   }
   if (diff &lt; hour) {
      return {tAgo: Math.floor(diff / minute) + " minutes ago", timeCheck: false};
   }
   if (diff &lt; hour * 2) {
      return {tAgo: "about 1 hour ago", timeCheck: false};
   }
   if (diff &lt; day) {
      return {tAgo: Math.floor(diff / hour) + " hours ago", timeCheck: true};
   }
   if (diff &gt; day && diff &lt; day * 2) {
      return {tAgo: "yesterday", timeCheck: true};
   }
   if (diff &lt; day * 365) {
      return {tAgo: Math.floor(diff / day) + " days ago", timeCheck: true};
   }
   else {
      return {tAgo: "over a year ago", timeCheck: true};
   }
}
Here is how i'd approach it

// Make all those units constants
var SECOND = 1000;
var MINUTE = SECOND * 60;
var HOUR = MINUTE * 60;
var DAY = HOUR * 24;
var YEAR = DAY * 365;

// store the string variations in a table, seperate from the code
TIMES = [
    {min: 0, text: "right now", unit: SECOND},
    {min: 2 * SECOND, text: "@ seconds ago", unit: SECOND},
    {min: MINUTE, text: "about 1 minute ago", unit: MINUTE},
    {min: 2 * MINUTE, text: "@ minutes ago", unit: MINUTE},
    {min: HOUR, text: "about 1 hour ago", unit: HOUR},
    {min: 2 * HOUR, text: "@ hours ago", unit: HOUR},
    {min: DAY, text: "yesterday", unit: DAY},
    {min: 2 * DAY, text: "@ days ago", unit: DAY},
    {min: YEAR, text: "over a year ago", unit: YEAR}
];

// use the table to generate the date
function timeAgoText(diff) {
    var use;
    for(var index = 0, length = TIMES.length; index &lt; length; index++)
    {
        if( TIMES[index].min &lt; diff )
        {
            use = TIMES[index];
        }
    }
    units = Math.floor(diff / use.unit)
    return use.text.replace("@", units)
}

function timeAgo(dateText)
{
   var then = new Date(dateString);
   if ($.browser.msie) {
      then = Date.parse(dateString.replace(/( \+)/, ' UTC$1'));
   }
   var diff = new Date() - then;
   if( isNaN(diff) || diff &lt; 0 )
   {
       return ""
   }
   else
   {
       // seperate the time check and the text generation
       return {
           tAgo: timeAgoText(diff),
           timeCheck: diff &gt; HOUR * 2 
       }
   }
}