Advertisement
Guest User

timeFormat.js

a guest
Apr 18th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //problem: the output is off, a freshly generated timestamp will give like -25 seconds.
  2. //time = 1429378522.32 <- python time.time() stored in a database
  3. function genTime(time) {
  4.     var total_seconds = +new Date()/1000 - time; //divide date by 1000 then subtract the old time
  5.         var MINUTE  = 60;
  6.         var HOUR    = MINUTE * 60;
  7.         var DAY     = HOUR * 24;
  8.     var WEEKS   = DAY * 7;
  9.     var MONTHS  = WEEKS * 4;
  10.     var YEARS   = MONTHS * 12;
  11.        
  12.     var years   = Math.floor( total_seconds / YEARS);
  13.         var days    = Math.floor( total_seconds / DAY );
  14.     var weeks   = Math.floor( total_seconds / WEEKS );
  15.     var months  = Math.floor( total_seconds / MONTHS);
  16.         var hours   = Math.floor( ( total_seconds % DAY ) / HOUR );
  17.         var minutes = Math.floor( ( total_seconds % HOUR ) / MINUTE );
  18.         var seconds = Math.floor( total_seconds % MINUTE );
  19.  
  20.         string = {};
  21.         if (days > 0) {
  22.         string.day = days + " " + (days == 1 && "day" || "days" );
  23.         }
  24.         if (hours > 0)
  25.                 string.hours = hours + " " + (hours == 1 && "hour" || "hours" );
  26.         if (minutes > 0) {
  27.                 string.minutes = minutes + " " + (minutes == 1 && "minute" || "minutes" );
  28.         }
  29.     if (weeks > 0) {
  30.         string.weeks = weeks + " " + (weeks == 1 && "week" || "weeks" );
  31.         }
  32.     if (months > 0) {
  33.         string.months = months + " " + (weeks == 1 && "week" || "weeks" );
  34.         }
  35.     if (years > 0) {
  36.         string.years = + " " + (years == 1 && "year" || "years");
  37.            
  38.         }
  39.     string.seconds = seconds + " " + (seconds == 1 && "second" || "seconds" );
  40.         return string;
  41. }
  42.  
  43. function Time(time, one) {
  44.     var string = "";
  45.     one = one || 1;
  46.    
  47.     if (one==0) {
  48.         var time = genTime(time);
  49.         if (time.years >=1) {
  50.             return time.years;
  51.         }
  52.         if (time.months >= 1) {
  53.             return time.months;
  54.         }
  55.         if (time.weeks <= 4) {
  56.             return time.weeks;
  57.         }
  58.         if (time.days) {
  59.             return time.days;
  60.         }
  61.         if (time.hours) {
  62.             return time.hours;
  63.         }
  64.         if (time.minutes) {
  65.             return time.minutes
  66.         }
  67.         if (time.seconds){
  68.             return time.seconds;
  69.         } else {
  70.             return 0 + 'S';
  71.         }
  72.  
  73.     }
  74.     if (one == 1) {
  75.         var time = genTime(time);
  76.         if (time.years){
  77.             string = time.years + ", ";
  78.         }
  79.         if (time.months) {
  80.             string = string + time.months + ", ";
  81.         }
  82.         if (time.days) {
  83.             string = string + time.days + ", ";
  84.         }
  85.         if (time.minutes) {
  86.             string = string + time.minutes + ", ";
  87.         }
  88.         if (time.seconds) {
  89.             string = string + time.seconds;
  90.         }
  91.        
  92.     return string;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement