js将毫秒数转化为天数:JavaScript日期格式化(1)

返回给定毫秒数的可读格式

思路实现代码

const formatDuration = ( ms ) => { if(ms < 0) ms = -ms; let time = { day: Math.floor(ms / 86400000), hour: Math.floor(ms / 3600000) % 24, minute: Math.floor(ms / 60000) % 60 } return Object.entries(time) .filter(val => val[1] !== 0) .map(([key, val])=>{ if(key === 'day') return `${val}天` if(key === 'hour') return `${val}时` if(key === 'minute') return `${val}分` }) .join(' '); }

测试代码

let t1 = formatDuration(62341001); console.log(t1); let t2 = formatDuration(34325055574); console.log(t2);

测试结果

17时 19分 397天 6时 44分

代码实现参阅:formatDuration - 30 seconds of code

,