Advertisement
Guest User

Carlos K

a guest
Jun 3rd, 2010
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Twitter like N min/sec ago timestamp in JS
  2. function timeSince(postDate) {
  3.     var i, j, name, name2, seconds, seconds2, count, count2,
  4.         chunks = [ // array of time period chunks
  5.             [60 * 60 * 24 * 365, 'year'],
  6.             [60 * 60 * 24 * 7, 'week'],
  7.             [60 * 60 * 24, 'day'],
  8.             [60 * 60, 'hour'],
  9.             [1 * 60, 'minute'],
  10.             [1 * 1, 'second']
  11.         ],
  12.         today = Math.round(new Date().getTime() / 1000),
  13.         since = today - parseInt(postDate, 10);
  14.     for (i = 0, j = chunks.length; i < j; i += 1) {
  15.         seconds = chunks[i][0];
  16.         name = chunks[i][1];
  17.         if ((count = Math.floor(since / seconds)) != 0) { // finding the biggest chunk (if the chunk fits, break)
  18.             break;
  19.         }
  20.     }
  21.     print = (count == 1) ? '1 ' + name + ",": count + " " + name + "s";
  22.     if (i + 1 < j - 1) { // now getting the second item
  23.         seconds2 = chunks[i + 1][0];
  24.         name2 = chunks[i + 1][1];
  25.         if ((count2 = Math.floor((since - (seconds * count)) / seconds2)) != 0) {
  26.             print += (count2 == 1) ? ', 1 ' + name2 : " " + count2 + " " + name2 + "s";
  27.         }
  28.     }
  29.     return print;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement