JS时间戳转为yyyy-mm-dd hh:mm:ss
时间: 2023-07-22 22:59:24 浏览: 64
可以使用以下代码将 JavaScript 时间戳转换为 yyyy-mm-dd hh:mm:ss 格式的日期和时间:
```javascript
function timestampToDatetime(timestamp) {
const date = new Date(timestamp);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "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");
const seconds = String(date.getSeconds()).padStart(2, "0");
const formattedDatetime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
return formattedDatetime;
}
const timestamp = 1623342000000; // 示例时间戳
const formattedDatetime = timestampToDatetime(timestamp);
console.log(formattedDatetime);
```
请将 `timestamp` 替换为你要转换的实际时间戳。这段代码将输出格式为 `yyyy-mm-dd hh:mm:ss` 的日期和时间字符串。