Ralph Holzmann

Web Development – jQuery, PHP, MySQL. In that order.

Relative Date String in JavaScript December 14th, 2009

Here’s a script I wrote awhile ago that’s used in this site and a few other projects of mine. It’s a method attached to the JavaScript Date object to output the date relative to now. It works for both past and future dates.

Date.prototype.toRelativeString = function() {

  // Calculate difference between date and now
  var today = parseInt((new Date()).getTime() / 1000);
  var date = parseInt(this.getTime() / 1000);
  var second_difference = today - date;
  var day_difference = Math.floor(second_difference / 86400);
  var when_text, one_off;

  // Determine if date is in future or past
  if (second_difference < 0) {
    when_text = ' from now';
    one_off = 'Tomorrow';
    second_difference = -second_difference;
    day_difference = -day_difference
  } else {
    one_off = 'Yesterday';
    when_text = ' ago';
  }

  // Return relative string
  if(day_difference == 0) {
      if(second_difference < 60) {
          return 'Just now';
      } else if(second_difference < 120) {
          return '1 minute' + when_text;
      } else if(second_difference < 3600) {
          return Math.floor(second_difference/60) + ' minutes' + when_text;
      } else if(second_difference < 7200) {
          return '1 hour' + when_text
      } else if(second_difference < 86400) {
          return Math.floor(second_difference/3600) + ' hours' + when_text;
      }
  } else if(day_difference == 1) {
      return one_off;
  } else if(day_difference < 7) {
      return day_difference + ' days' + when_text;
  } else if(day_difference == 7) {
      return '1 week' + when_text;
  } else if(day_difference < (7*6)) {
      return Math.ceil(day_difference/7) + ' weeks' + when_text;
  } else if(day_difference < 365) {
      return Math.ceil(day_difference/(365/12)) + ' months' + when_text
  } else {
      years = Math.round(day_difference/365);
      return years + ' year' + (years != 1 ? 's' : '') + when_text;
  }
};

Here’s some examples:

var now = new Date();
document.write(now.toRelativeString() + '<br />'); // Just now

now.setDate(parseInt(now.getDate()) + 3);
document.write(now.toRelativeString() + '<br />'); // 3 days from now

now.setDate(parseInt(now.getDate()) - 4);
document.write(now.toRelativeString() + '<br />'); // Yesterday

now.setDate(parseInt(now.getDate()) - 4);
document.write(now.toRelativeString() + '<br />'); // 5 days ago

now.setDate(parseInt(now.getDate()) - 7);
document.write(now.toRelativeString() + '<br />'); // 2 weeks ago

now.setFullYear(parseInt(now.getFullYear()) - 7);
document.write(now.toRelativeString() + '<br />'); // 7 years ago

now.setFullYear(parseInt(now.getFullYear()) + 10);
document.write(now.toRelativeString() + '<br />'); // 3 years from now

It comes in very handy when dealing with dates. Hope you enjoy and please let me know if there are any bugs.

0 CommentsTime: Relative | Actual

Other stuff …

Tweets I write

Posts I write

Companies I currently develop for

Past development gigs

Things I use everyday

Ways to contact me