node.jsmomentjs

Convert moment time to milliseconds


I want to convert moment time to milliseconds. I have next code:

const moment = require("moment")

const countdown = "00:02:32"
const millis = moment(countdown, "hh:mm:ss")
console.log(millis.milliseconds())

As a result I get 0

Idea is I have some countdown and I want to know hom many milliseconds I need to wait for next operation.


Solution

  • moment.duration(countdown, "HH:mm:ss") creates a duration object from the countdown string.

    duration.asMilliseconds() converts the duration into milliseconds.

    const moment = require("moment");
    
    const countdown = "00:02:32";
    const duration = moment.duration(countdown, "HH:mm:ss");
    
    const millis = duration.asMilliseconds();
    console.log(millis); // Output will be 152000 milliseconds (or 2 minutes and 32 seconds)