const timestampToTime= (timestamp) => {
const date = new Date(timestamp * 1000);
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
const hour = date.getHours().toString().padStart(2, '0');
const minute = date.getMinutes().toString().padStart(2, '0');
const second = date.getSeconds().toString().padStart(2, '0');
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}
const timestamp = 1679905673;
const timeString = timestampToTime(timestamp);
console.log(timeString); // 输出格式化后的时间字符串:2023-03-27 16:27:53
标签:const,timestamp,JavaScript,padStart,转化,时间,date,toString
From: https://www.cnblogs.com/zxingduo/p/17336301.html