// 获取当前时间戳
function converTime(timestamp: number) {
const currentTimestamp = new Date().getTime();
const timeDifference = (currentTimestamp - timestamp) / 1000; // 转换为秒
if (timeDifference <= 0) {
return '刚刚';
} else if (timeDifference < 60) {
return `${Math.floor(timeDifference)}秒前`;
} else if (timeDifference < 3600) {
return `${Math.floor(timeDifference / 60)}分钟前`;
} else if (timeDifference < 86400) {
return `${Math.floor(timeDifference / 3600)}小时前`;
} else if (timeDifference < 604800) {
return `${Math.floor(timeDifference / 84600)}天前`;
} else if (timeDifference < 2592000) {
return `${Math.floor(timeDifference / 604800)}周前`;
} else if (timeDifference < 31536000) {
return `${Math.floor(timeDifference / 2592000)}个月前`;
} else {
return `${Math.floor(timeDifference / 31536000)}年前`;
}
}
// 传入2021-04-01 00:00:00的时间戳
converTime(new Date('2021-04-01 00:00:00').getTime())
根据传入时间戳计算距离当前时间多久
于 2024-09-02 20:59:30 首次发布