formatDuration.js 692 B

123456789101112131415161718192021222324
  1. /**
  2. * 格式化video通话时间
  3. * @export
  4. * @param {number} int
  5. * @returns
  6. */
  7. function formatInt(int) {
  8. return int < 10 ? `0${int}` : int
  9. }
  10. export function formatDuration(duration) {
  11. if (duration < 60) {
  12. return `00:00:${formatInt(duration)}`
  13. }
  14. if (duration < 60 * 60) {
  15. const min = parseInt(duration / 60)
  16. const sec = duration - min * 60
  17. return `00:${formatInt(min)}:${formatInt(sec)}`
  18. }
  19. const hour = parseInt(duration / (60 * 60))
  20. const remainder = duration - hour * (60 * 60)
  21. const min = parseInt(remainder / 60)
  22. const sec = remainder - min * 60
  23. return `${formatInt(hour)}:${formatInt(min)}:${formatInt(sec)}`
  24. }