AroliS

how to get a date from a previous date in seconds in nodejs

Feb 22nd, 2020
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Created by AroliS^
  3. You are not allow to publish this code as your
  4. All credits goes to me.
  5. */  
  6.  
  7. //example code
  8. timeFormat( old.timeStamp - new.timeStamp );
  9.  
  10. //function
  11.   function timeFormat( ms )
  12.     {
  13.         var years
  14.             , months
  15.             , days
  16.             , hours
  17.             , mins  
  18.             , secs;
  19.  
  20.         secs    = parseInt( ms     * 0.001,     10 );
  21.         mins    = parseInt( secs   * 0.0166667, 10 );
  22.         hours   = parseInt( mins   * 0.0166667, 10 );
  23.         days    = parseInt( hours  * 0.0416667, 10 );
  24.         months  = parseInt( days   * 0.0328767, 10 );
  25.         years   = parseInt( months * 0.0833334, 10 );
  26.        
  27.         years   = parseInt( (years%31536000), 10);
  28.         months  = parseInt( (months%2678400), 10);
  29.         days    = parseInt( (days%86400), 10);
  30.         hours   = parseInt( (hours%3600), 10 );
  31.         minutes = parseInt( (mins%60), 10 );
  32.         secs    = parseInt( (secs%60), 10 );
  33.        
  34.  
  35.         if ( years  != 0 ) return `${years} year(s)`;
  36.         if ( months != 0 ) return `${months} month(s)`;
  37.         if ( days   != 0 ) return `${days} day(s)`;
  38.         if ( hours  != 0 ) return `${hours} hour(s)`;
  39.         if ( mins   != 0 ) return `${mins} min(s)`;
  40.         if ( secs   != 0 ) return `${secs} sec(s)`;
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment