Question

I'm trying to output a dynamic figure in seconds to: seconds, minutes, hours or days depending on the total.

What would be the best way to quickly format the seconds such as below?

33 seconds = 33 seconds
92 seconds = 1.5 minutes
6200 seconds = 1.7 hours
180000 seconds = 2 days
980010 seconds = 1.6 weeks

I was hoping there was a jQuery library that could handle this?

Any help is much appreciated.

Was it helpful?

Solution

The function below works quite well. You can add some conditional statements within the function to return the exact string you want. i.e just the seconds if all the other values are ==0 etc.

function convertFromSeconds(seconds)
{

var numdays = Math.floor(seconds / 86400);
var numhours = Math.floor((seconds % 86400) / 3600);
var numminutes = Math.floor(((seconds % 86400) % 3600) / 60);
var numseconds = ((seconds % 86400) % 3600) % 60;

return numdays + " days " + numhours + " hours " + numminutes + " minutes " + numseconds + " seconds";

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top