toTimeString.js 604 B

123456789101112131415161718192021222324
  1. var countSteps = require('../math/countSteps');
  2. var pad = require('../number/pad');
  3. var HOUR = 3600000,
  4. MINUTE = 60000,
  5. SECOND = 1000;
  6. /**
  7. * Format timestamp into a time string.
  8. */
  9. function toTimeString(ms){
  10. var h = ms < HOUR ? 0 : countSteps(ms, HOUR),
  11. m = ms < MINUTE ? 0 : countSteps(ms, MINUTE, 60),
  12. s = ms < SECOND ? 0 : countSteps(ms, SECOND, 60),
  13. str = '';
  14. str += h? h + ':' : '';
  15. str += pad(m, 2) + ':';
  16. str += pad(s, 2);
  17. return str;
  18. }
  19. module.exports = toTimeString;