之前在一個(gè)小程序項(xiàng)目中遇到了類似的問(wèn)題觅够,當(dāng)時(shí)處理辦法是按照一般邏輯寫的陶因,根本沒想那么多骡苞。代碼如下:
formatTime(time) {
var days = parseInt(time / (1000 * 60 * 60 * 24));
var hours = parseInt((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = parseInt((time % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((time % (1000 * 60)) / 1000);
this.setData({
days,
hours,
minutes,
seconds
})
}
后來(lái)在公司另一個(gè)項(xiàng)目中又遇到了這個(gè)問(wèn)題,我想了想楷扬,要是按照一般邏輯解幽,感覺會(huì)寫很長(zhǎng)一串加減乘除,會(huì)不會(huì)有簡(jiǎn)單點(diǎn)的方法烘苹,于是下面這種方法就誕生了躲株。
formatTime(msTime) {
let time = msTime /1000;
let day = Math.floor(time /60 /60 /24);
let hour = Math.floor(time /60 /60) %24;
let minute = Math.floor(time /60) %60;
let second = Math.floor(time) %60;
return `${day}天${hour}時(shí)${minute}分${second}秒`
}