3

I'm trying to fetch the duration of each mp3 sound but sometimes it displays the duration while other times it shows invalid.

I want to print the time in hh:mm:ss format.

Div Block where i'm showing duration:

var duration17 = document.getElementById("audio-17").duration;
        duration17 = secondstotime(duration17);
        document.getElementById("duration-17").innerHTML = duration17;

My function for converting seconds to proper time:

//convert seconds to the proper time duration
    function secondstotime(secs)
    {
        var t = new Date(1970,0,1);
        t.setSeconds(secs);
        var s = t.toTimeString().substr(0,8);
        if(secs > 86399)
            s = Math.floor((t - Date.parse("1/1/70")) / 3600000) + s.substr(2);
        return s;
    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • What kind of output are you looking for here? If you want times like `61 seconds = 1:01` then that function looks like a really complicated and error-prone way to do it... – brabster Apr 30 '18 at 15:06
  • 1
    output should be something like hour:min:ss i.e 00:10:13 – Dev Troubleshooter Apr 30 '18 at 15:06
  • 1
    this might help you https://stackoverflow.com/questions/1322732/convert-seconds-to-hh-mm-ss-with-javascript – Andrew Lohr Apr 30 '18 at 15:08
  • That question has the answer. Suggest `moment.js` if you can use it, or the one-line solution in there if you can't. Nice @AndrewLohr – brabster Apr 30 '18 at 15:10
  • @brabster no i tried momentjs too but still its throwing this error. – Dev Troubleshooter Apr 30 '18 at 15:10
  • I think you'll be dealing with a `duration` in `momentjs` https://momentjs.com/docs/#/durations/ - I'll have a quick look for an example... – brabster Apr 30 '18 at 15:13
  • @brabster—it is really annoying and utterly unhelpful to see every question even remotely connected with dates or time answered with "use moment.js". It's like every question on DOM being answered with "use jQuery". The OP's problem can be solved with 1 line of code and has been answered here many, many times before, it doesn't need yet another library. – RobG Apr 30 '18 at 22:10
  • @RobG there were multiple choices - Andrew linked to a question that had those answers and they were available to the OP as well. The OP was still struggling so I helped them figure it out. Annoying maybe, but I think the OP was helped and that's the main thing. If there's more discussion to be had then meta or chat is probably more appropriate. – brabster May 01 '18 at 08:04

2 Answers2

2

OK, tricker than I expected with momentjs as it has no duration formatting. A quick workaround is:

const duration =  moment.duration(61, 'seconds');
const formatted = moment.utc(duration.asMilliseconds()).format("HH:mm:ss");


> formatted
'00:01:01'

I think you can replace the code you have in secondstotime with something based on the above without much trouble.

It's also pretty straightforward to produce a HH:mm:ss formatted duration from a number of seconds using maths yourself - see the linked question Convert seconds to HH-MM-SS with JavaScript? for alternative approaches. If this is the only thing you need to do with dates and times then a one-liner might be a better solution than bringing a library like momentjs in.

That should simplify things and eliminate most possible errors. If you're still seeing an error, could you log the value of the input to secondstotime and share it?

brabster
  • 42,504
  • 27
  • 146
  • 186
1

Supposing your duration is seconds, then you can simply convert it to hh:mm:ss using the following. Using a library like moment.js for such a trivial task is serious overkill. If support for negative values is needed, a small change is necessary.

/* @param {number} secs - duration in seconds
** @returns {string} duration in hh:mm:ss format
*/
function secsToTime(secs) {
  return [secs/3600|0, (secs%3600)/60|0, secs%60].map(n=>(n<10?'0':'')+n).join(':');
}

// Sample
[0,23,61,1024,3600,123169].forEach(s=>
  console.log(s + ': ' + secsToTime(s)));
  
RobG
  • 142,382
  • 31
  • 172
  • 209