Home > Mobile >  How to convert mm:ss to seconds using momentjs?
How to convert mm:ss to seconds using momentjs?

Time:11-11

I am using reactjs. On the frontend I am getting the time in format "mm:ss" example data:

const data [{
  duration: "00:10" //I want 10
}, {
  duration: "20:00" //I want 1200
}, {
  duration: "30:00"  // I want 1800
}]

I tried this and a lot but could not work

moment.duration("30:00").asSeconds()

These duration are in mm:ss format and I want them to convert to just seconds. How can I do that either using Javascript or moment js?

Thanks

CodePudding user response:

That good moment handle all thing . try this it would work.

moment.duration("whatever time is").asSeconds()

or

 const Second=(new Date(moment.duration *.1000)).toUTCString().match(/(\d\d:\d\d:\d\d)/)[0];

CodePudding user response:

The format required for the duration method is hh:mm:ss. So your code should look something like this:

moment.duration("00:30:00").asSeconds()

Note: I tested this on [email protected] and it's working as expected.

CodePudding user response:

In case you don't want to add hours for the duration to work, you can try this:

moment('30:00', 'mm:ss').diff(moment().startOf('day'), 'seconds');
  • Related