Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert milliseconds into a readable date Minutes:Seconds Format?

Tags:

javascript

In JavaScript I have a variable Time in milliseconds.

I would like to know if there is any build-in function to convert efficiently this value to Minutes:Seconds format.

If not could you please point me out a utility function.

Example:

FROM

462000 milliseconds

TO

7:42
like image 365
GibboK Avatar asked Nov 28 '12 09:11

GibboK


4 Answers

Just create a Date object and pass the milliseconds as a parameter.

var date = new Date(milliseconds);
var h = date.getHours();
var m = date.getMinutes();
var s = date.getSeconds();
alert(((h * 60) + m) + ":" + s);
like image 130
Gerrit Bertier Avatar answered Oct 20 '22 12:10

Gerrit Bertier


Thanks guys for your support, at th end I came up with this solution. I hope it can helps others.

Use:

var videoDuration = convertMillisecondsToDigitalClock(18050200).clock; // CONVERT DATE TO DIGITAL FORMAT

// CONVERT MILLISECONDS TO DIGITAL CLOCK FORMAT
function convertMillisecondsToDigitalClock(ms) {
    hours = Math.floor(ms / 3600000), // 1 Hour = 36000 Milliseconds
    minutes = Math.floor((ms % 3600000) / 60000), // 1 Minutes = 60000 Milliseconds
    seconds = Math.floor(((ms % 360000) % 60000) / 1000) // 1 Second = 1000 Milliseconds
        return {
        hours : hours,
        minutes : minutes,
        seconds : seconds,
        clock : hours + ":" + minutes + ":" + seconds
    };
}
like image 26
GibboK Avatar answered Oct 20 '22 13:10

GibboK


In case you already using Moment.js in your project, you can use the moment.duration function

You can use it like this

var mm = moment.duration(37250000);
console.log(mm.hours() + ':' + mm.minutes() + ':' + mm.seconds());

output: 10:20:50

See jsbin sample

like image 3
Daniel Avatar answered Oct 20 '22 12:10

Daniel


It's easy to make the conversion oneself:

var t = 462000
parseInt(t / 1000 / 60) + ":" + (t / 1000 % 60)
like image 2
August Karlstrom Avatar answered Oct 20 '22 11:10

August Karlstrom