/**
* 格式化时间
* @param seconds 秒
*/
export function formatDuration(seconds: number): string {
const years = Math.floor(seconds / (365 * 24 * 3600));
seconds %= (365 * 24 * 3600);
const months = Math.floor(seconds / (30 * 24 * 3600));
seconds %= (30 * 24 * 3600);
const days = Math.floor(seconds / (24 * 3600));
seconds %= (24 * 3600);
const hours = Math.floor(seconds / 3600);
seconds %= 3600;
const minutes = Math.floor(seconds / 60);
const secs = seconds % 60;
const parts = [];
if (years > 0) {
parts.push(`${years}年`);
}
if (months > 0) {
parts.push(`${months}月`);
}
if (days > 0) {
parts.push(`${days}天`);
}
if (hours > 0) {
parts.push(`${hours}小时`);
}
if (minutes > 0 || secs > 0 || parts.length === 0) {
parts.push(`${minutes}分钟`);
}
// 显示秒数,即使为零,如果它是唯一的单位
if (secs > 0 || parts.length === 0) {
parts.push(`${secs}秒`);
}
return parts.join(' ');
}
【ts格式化秒】输入秒返回年月日时分秒
于 2024-05-31 09:23:01 首次发布