1分鐘以內(nèi)顯示秒數(shù)
59秒
1分鐘以上1小時(shí)以內(nèi)顯示分鐘數(shù)和秒數(shù)
23分30秒
1小時(shí)以上1天以內(nèi)顯示小時(shí)數(shù)和分鐘數(shù)
1小時(shí)20分
1天以上顯示天數(shù)和小時(shí)數(shù)
3天21小時(shí)
核心代碼
export function simpleDuration(duration, type){
if(type==='s'){
duration = duration*1000
}
let days='', hours='', minutes='', seconds=''
let day = 24*60*60*1000,
hour = 60*60*1000,
minute = 60*1000,
second = 1000
if(duration>=day){
days = Math.floor(duration/day)+'天'
hours = Math.floor(duration%day/hour)+'小時(shí)'
}else if(duration>=hour&&duration<day){
hours = Math.floor(duration/hour)+'小時(shí)'
minutes = Math.floor(duration%hour/minute)+'分'
}else if(duration>minute&&duration<hour){
minutes = Math.floor(duration/minute)+'分'
seconds = Math.floor(duration%minute/second)+'秒'
}else if(duration<minute){
seconds = Math.floor(duration/second)+'秒'
}
return days+hours+minutes+seconds
}
結(jié)果是
show: 1天2小時(shí)
show2: 34秒