JavaScript 将 Unix 时间戳转换为 yyyy-MM-dd HH:mm:ss 格式
以下是将 Unix 时间戳转换为'yyyy-MM-dd HH:mm:ss' 格式的 JavaScript 代码:
function unixTimeToDateTime(unixTimestamp) {
let date = new Date(unixTimestamp * 1000);
let year = date.getFullYear();
let month = ('0' + (date.getMonth() + 1)).slice(-2);
let day = ('0' + date.getDate()).slice(-2);
let hours = ('0' + date.getHours()).slice(-2);
let minutes = ('0' + date.getMinutes()).slice(-2);
let seconds = ('0' + date.getSeconds()).slice(-2);
return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
}
使用示例:
let unixTimestamp = 1626978717;
let dateTime = unixTimeToDateTime(unixTimestamp);
console.log(dateTime); // 输出:2021-07-22 11:51:57
原文地址: https://www.cveoy.top/t/topic/ok5K 著作权归作者所有。请勿转载和采集!