문제

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.

도움이 되었습니까?

해결책

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";

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top