以毫秒为时间单位,计算当前毫秒为 **天 **时 **分 **秒
1.显示**天 **时 **分 **秒
主要代码:
time = 170276567;
getDuration(time) {
var days = time / 1000 / 60 / 60 / 24;
var daysRound = Math.floor(days);
var hours = time / 1000 / 60 / 60 - 24 * daysRound;
var hoursRound = Math.floor(hours);
var minutes = time / 1000 / 60 - 24 * 60 * daysRound - 60 * hoursRound;
var minutesRound = Math.floor(minutes);
var seconds = time / 1000 - 24 * 60 * 60 * daysRound - 60 * 60 * hoursRound - 60 * minutesRound;
var secondsRound = Math.floor(seconds);
var timeRound = "";
if (daysRound == 0) {
timeRound = hoursRound + "时" + minutesRound + "分" + secondsRound + "秒";
} else if (daysRound !== 0) {
timeRound = daysRound + "天" + hoursRound + "时" + minutesRound + "分" + secondsRound +"秒";
}
}
return timeRound;
},
结果:
2.显示**月**天 **时 **分 **秒
time=2921499440,
getDuration(time) {
// console.log(time,'time')
var month = time / 1000 / 60 / 60 / 24 / 30;//月
var monthRound = Math.floor(month);//月
var days = time / 1000 / 60 / 60 / 24 - 30 * monthRound;
var daysRound = Math.floor(days);
var hours = time / 1000 / 60 / 60 -24* 30 * monthRound- 24 * daysRound;
var hoursRound = Math.floor(hours);
var minutes = time / 1000 / 60 -24* 30 * 60* monthRound - 24 * 60 * daysRound - 60 * hoursRound;
var minutesRound = Math.floor(minutes);
var seconds = time / 1000 -24* 30 * 60*60* monthRound- 24 * 60 * 60 * daysRound - 60 * 60 * hoursRound - 60 * minutesRound;
var secondsRound = Math.floor(seconds);
var timeRound = "";
if (daysRound == 0) {
timeRound = hoursRound + "时" + minutesRound + "分" + secondsRound + "秒";
} else if ( monthRound == 0) {
timeRound =daysRound + "天" + hoursRound + "时" + minutesRound + "分" + secondsRound +"秒";
} else if (monthRound != 0){
timeRound =monthRound+"月"+ daysRound + "天" + hoursRound + "时" + minutesRound + "分" + secondsRound +"秒";
}
console.log('monthRound',monthRound)
return timeRound;
},
结果: