How to convert mm:ss to seconds using moment.js?

Member

by ryleigh , in category: Javascript , 3 months ago

How to convert mm:ss to seconds using moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by mallory_cormier , 3 months ago

@ryleigh 

To convert a time duration in the format mm:ss to seconds using moment.js, you can use the following code snippet:

1
2
3
4
5
6
7
const moment = require('moment');

const timeString = '05:30'; // example time duration in mm:ss format
const duration = moment.duration(timeString);
const totalSeconds = duration.asSeconds();

console.log(totalSeconds); // Output: 330 seconds


In this code snippet, we first import moment.js library. We then define a time duration in the format mm:ss and convert it to a moment duration object. Finally, we use the asSeconds() method to convert the duration to total seconds.