1

I am trying to correctly display a duration with moment.js, but I can't find the right way to do it.

I have two events, start and stop. Both record in a variable a video current time, in seconds (e.g. 254.36 and 428.12).

First, I want to display the start and stop time. I achieve that like this:

var startTime = 12345; //let's say it is my start time in seconds
moment.utc(startTime *1000).format("HH:mm:ss.SSS");

Second, I want to display the duration between start and stop in a human readable way. For example, if stopTime - startTime === 94,23 seconds, I want to:

  • omit the milliseconds. I could just use Math.round() on my subtraction result but I want to know if it can be done using moment.js.
  • present it in an accurate formated version. I started with something like that:

    moment.duration(stopTime - startTime , "seconds").humanize(); 
    

    but it will display 'a few seconds' if I have, for instance, a 4 second input. If I have a stopTime - startTime === 94,23 seconds, I would like to display something like: 1 minute and 34 seconds (and omit the minute if it lasts less than a minute).

Again, I could avoid using moment.js but I want to take advantage of the localization features, learn what can and cannot be done and how to use it properly.

Billybobbonnet
  • 3,156
  • 4
  • 23
  • 49
  • As I know there are no native way for momentjs to display only significant values, so try write small function to get right output format for different range values – Panoptik Aug 18 '15 at 13:42
  • @Ivan, you're right, that's a duplicate. I already quickly read the question, but I missed the essential. This is more or less my answer. I voted to close. – Billybobbonnet Aug 18 '15 at 20:14

0 Answers0