skip to Main Content

I’m using moment js to format milliseconds into a mm:ss format with the following code

moment(myVariable).format('mm:ss');

This works perfectly fine, however a requirement for the app I’m building needs to show 60 minutes as 60:00 rather than using hours like 01:00:00. I came across another post that suggested doing

moment.duration(myVariable, 'seconds').asMinutes();

however it formats 3,600,000 milliseconds into minutes as 60000 instead of 60:00. Is there a way to make it display as 60:00?

2

Answers


  1. You can generate string manually:

    var d = moment.duration(3600000); // in ms
    // slice used for add leading zero
    var str = `${d.hours() * 60 + d.minutes()}:${('0' + d.seconds()).slice(-2)}`;
    
    Login or Signup to reply.
  2. Alternatively, you can consider using .diff method:

    const m = moment(myVariable);
    `${m.diff(0, 'minutes').padStart(2, '0')}:${m.format('ss')}`;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search