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

Member

by ryleigh , in category: Javascript , a year ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by mallory_cormier , a year 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.

Related Threads:

How to find time difference in seconds using moment.js?
How to convert date to hours from now using moment.js?
How to convert new date() to utc date using moment.js?
How to convert seconds to hours in mysql?
How to convert a column in seconds to dateformat in teradata?
How to redirect after 5 seconds in PHP?