const formatDuration = ms => {
if (ms < 0) ms = -ms;
const time = {
day: Math.floor(ms / 86400000),
hour: Math.floor(ms / 3600000) % 24,
minute: Math.floor(ms / 60000) % 60,
second: Math.floor(ms / 1000) % 60,
millisecond: Math.floor(ms) % 1000
};
return Object.entries(time)
.filter(val => val[1] !== 0)
.map(([key, val]) => `${val} ${key}${val !== 1 ? 's' : ''}`)
.join(', ');
};
【Format duration】Returns the human-readable format of the given number of milliseconds.
最新推荐文章于 2025-11-24 03:12:01 发布
这是一个JavaScript函数,用于将毫秒数转换为格式化的时长字符串,包括天、小时、分钟、秒和毫秒。它首先检查输入是否为负数,然后计算各个时间单位,并过滤掉值为0的单位,最后返回一个格式化的字符串。
702

被折叠的 条评论
为什么被折叠?



