Convert time to jQuery

From the youtube player http://code.google.com/apis/ajax/playground/?exp=youtube#chromeless_player I get the time value in seconds, for example "243.577". Let it be a simple line.

How to convert it to a value like: '04: 35 '? Like 4 minutes and 35 seconds (hope I made the right calculations) for this example.

If the value is only 5 seconds, then it should give something like "00: 05". If negative, then "00: 00".

+4
source share
3 answers
var raw = "-54"; var time = parseInt(raw,10); time = time < 0 ? 0 : time; var minutes = Math.floor(time / 60); var seconds = time % 60; minutes = minutes < 10 ? "0"+minutes : minutes; seconds = seconds < 10 ? "0"+seconds : seconds; alert(minutes+":"+seconds); 

Working demo: http://jsfiddle.net/8zPRF/

UPDATE

Some added lines for negative numbers and string format: http://jsfiddle.net/8zPRF/3/

+11
source

You can do something like

 var d = new Date(milliseconds); 

You do not need jQuery for this.

+1
source

I found that the Date.js library is extremely useful when dealing with dates. It extends the built-in javascript date object.

eg.

 var timeString = new Date(seconds * 1000).toString('mm:ss'); 
0
source

All Articles