Home > Mobile >  Format hhmmss to 00:00:00
Format hhmmss to 00:00:00

Time:07-12

I found this code that gets the duration of an MP3 file but the problem is it shows the output of duration as 00:00. How difficult would it be to show the duration as 0:00:00?

    var audio_url_raw = document.querySelector('.acf-fields.-top div#url.acf-field.acf-field-text.acf-field-60db6e20efbb0 div.acf-input div.acf-input-wrap input');
    var au = document.createElement('audio');
    au.src = audio_url_raw.value;
    au.addEventListener('loadedmetadata', function(){
        var toHHMMSS = (secs) => {
        var sec_num = parseInt(secs, 10)
        var hours   = Math.floor(sec_num / 3600)
        var minutes = Math.floor(sec_num / 60) % 60
        var seconds = sec_num % 60
        return [hours,minutes,seconds]
            .map(v => v < 10 ? "0"   v : v)
            .filter((v,i) => v !== "00" || i > 0)
            .join(":")
        }
        var duration = au.duration;
        $(".acf-fields.-top div#duration.acf-field.acf-field-text.acf-field-60db6e92efbb2 div.acf-input div.acf-input-wrap input").val(toHHMMSS(duration));
    },false);

CodePudding user response:

Just comment out the filter

Also I would move the function outside the event listener

If you want the hour as a single digit, then more code is needed

const toHHMMSS = (secs) => {
  var sec_num =  secs;
  var hours = Math.floor(sec_num / 3600)
  var minutes = Math.floor(sec_num / 60) % 60
  var seconds = sec_num % 60
  return [hours, minutes, seconds]
    .map(v => v < 10 ? "0"   v : v)   // or String(v).padStart(2,"0")
//    .filter((v, i) => v !== "00" || i > 0)
    .join(":")
}

console.log(
  toHHMMSS(100)
)
console.log(
  toHHMMSS("120")
)

  • Related