Advertisement
kotvalera83

Untitled

Sep 22nd, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function xTimeAgo(time) {
  2.     var nd = new Date();
  3.     //var gmtDate = Date.UTC(nd.getFullYear(), nd.getMonth(), nd.getDate(), nd.getHours(), nd.getMinutes(), nd.getMilliseconds());
  4.     var gmtDate = Date.parse(nd);
  5.     var tweetedTime = Date.parse(time);
  6.     var timeDiff = (gmtDate - tweetedTime) / 1000;
  7.     //convert milliseconds to seconds
  8.     var second = 1, minute = 60, hour = 60 * 60, day = 60 * 60 * 24, week = 60 * 60 * 24 * 7, month = 60 * 60 * 24 * 30, year = 60 * 60 * 24 * 365;
  9.  
  10.     if (timeDiff > second && timeDiff < minute) {
  11.             return Math.round(timeDiff / second) + " second"+oneOreMany(Math.round(timeDiff / second))+" ago";
  12.     } else if (timeDiff >= minute && timeDiff < hour) {
  13.         return Math.round(timeDiff / minute) + " minute"+oneOreMany(Math.round(timeDiff / minute))+" ago";
  14.     } else if (timeDiff >= hour && timeDiff < day) {
  15.         return Math.round(timeDiff / hour) + " hour"+oneOreMany(Math.round(timeDiff / hour))+" ago";  
  16.     } else if (timeDiff >= day && timeDiff < week) {
  17.         return Math.round(timeDiff / day) + " day"+oneOreMany(Math.round(timeDiff / day))+" ago";
  18.     } else if (timeDiff >= week && timeDiff < month) {
  19.         return Math.round(timeDiff / week) + " week"+oneOreMany(Math.round(timeDiff / week))+" ago";          
  20.     } else if (timeDiff >= month && timeDiff < year) {
  21.         return Math.round(timeDiff / month) + " month"+oneOreMany(Math.round(timeDiff / month))+" ago";        
  22.     } else {
  23.         return 'over a year ago';
  24.     }
  25. }
  26.  
  27. function oneOreMany (arg) {
  28.     if (arg == 1) {
  29.         return '';
  30.     } else{
  31.         return 's';
  32.     };
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement