我试图将总秒数转换为使用jQuery并挣扎的时间。
我有一个示例:来自外部来源的{"expires": "254254"}
响应,我想将其转换为2days Xhours Xminutes left
,我尝试过使用类似的内容:
function timeDifference(elapsed) {
var msPerMinute = 60 * 1000;
var msPerHour = msPerMinute * 60;
var msPerDay = msPerHour * 24;
var msPerMonth = msPerDay * 30;
var msPerYear = msPerDay * 365;
if (elapsed < msPerMinute) {
return Math.round(elapsed/1000) + ' seconds ago';
} else if (elapsed < msPerHour) {
return Math.round(elapsed/msPerMinute) + ' minutes ago';
} else if (elapsed < msPerDay ) {
return Math.round(elapsed/msPerHour ) + ' hours ago';
} else if (elapsed < msPerMonth) {
return 'approximately ' + Math.round(elapsed/msPerDay) + ' days ago';
} else if (elapsed < msPerYear) {
return 'approximately ' + Math.round(elapsed/msPerMonth) + ' months ago';
} else {
return 'approximately ' + Math.round(elapsed/msPerYear ) + ' years ago';
}
}
答案 0 :(得分:1)
<强> - 更新强>
function DDHHMMSS(seconds){
var t = seconds
var cd = 24 * 60 * 60 * 1000,
ch = 60 * 60 * 1000,
cs = 60 * 1000,
d = Math.floor(t / cd),
h = Math.floor( (t - d * cd) / ch),
m = Math.round( (t - d * cd - h * ch) / 60000),
s = Math.round( (t - d * cd - h * ch ) / 360000),
pad = function(n){ return n < 10 ? '0' + n : n; };
if( m === 60 ){
h++;
m = 0;
}
if( h === 24 ){
d++;
h = 0;
}
var days = d.toString() + "days";
var hour = pad(h).toString() + "Hours";
var minutes = pad(m).toString() + "Minutes";
var Seconds = pad(s).toString() + "Seconds";
return [days , hour , minutes ,Seconds].join(':');
}
- 更新
使用datejs,只需写下:
(new Date).clearTime()
.addSeconds(15457)
.toString('H:mm:ss');
- 更新
现在date.js已过时且未维护,因此请使用“Moment.js”,
答案 1 :(得分:0)