JavaScript

【JavaScript】ミリ秒をhh:mm:ss,sss形式に変換する

100時間以上は非対応

function msToTime(duration) {
  /*
    1minute = 60000ms
    1hour = 60minutes = 3600000ms
   */
  const hour = Math.floor(duration / 3600000);
  const minute = Math.floor((duration - 3600000 * hour) / 60000);

  const hh = ('00' + hour).slice(-2);
  const mm = ('00' + minute).slice(-2);
  const ms = ('00000' + (duration % 60000)).slice(-5);

  const time = `${hh}:${mm}:${ms.slice(0,2)},${ms.slice(2,5)}`;

  return time
}

参考