Onestay
Onestay

Reputation: 498

Actively convert seconds to milliseconds in javascript

I currently have a setup that makes seconds and converts them to a H:mm:ss format using moment duration format.

formatSeconds(sec) {
    return moment.duration(sec, 'seconds').format('H:mm:ss', { trim: false });
}

the amount of seconds comes from my backend server. Now I also want to get the milliseconds only using the seconds.

I have tried this but it doesn't seem to work.

formatMilliseconds(sec) {
    return moment.duration(sec, 'seconds').format('H:mm:ss:S', { trim: false });
}

Upvotes: 1

Views: 7098

Answers (1)

rasmeister
rasmeister

Reputation: 1996

If you pass in a number of seconds as an integer the milliseconds will always be 0. The only way to get milliseconds is to pass in a fraction of a second.

Example: formatMilliSeconds(32501.2)

Now the output is: 9:01:41:200

Upvotes: 1

Related Questions