Advertisement
isama92

durationToReadableTime

Jul 7th, 2020
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Convert seconds into a human readable string
  3.  * @param {number} duration Seconds
  4.  * @returns {string}
  5.  */
  6. const durationToReadableTime = duration => {
  7.     let tmp = duration / 60;
  8.     let minutes = Math.floor(tmp);
  9.     tmp = tmp - minutes;
  10.     const seconds = Math.round(tmp * 60);
  11.  
  12.     if (minutes >= 60) {
  13.         tmp = minutes / 60;
  14.         const hours = Math.floor(tmp);
  15.         tmp = tmp - hours;
  16.         minutes = Math.round(tmp * 60);
  17.         return `${hours}h ${minutes}m`;
  18.     }
  19.  
  20.     return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement