function formatTimestamp(timestamp) {
const date = new Date(timestamp * 1000); // 将秒级时间戳转换为毫秒级时间戳
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份是从0开始的
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}`;
}
// 示例
const timestamp = 1692195600; // 示例秒级时间戳
console.log(formatTimestamp(timestamp)); // 输出 "2024-08-16 17:00"
时间戳转换方法
最新推荐文章于 2024-09-03 17:02:58 发布