以下是将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
``
js把unix时间戳转yyyy-MM-dd HHmmss格式

原文地址: https://www.cveoy.top/t/topic/fFdP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录