| 以下是将时间戳转换为年月日时分秒的 JavaScript 代码: |
| |
复制
| function formatDate(timestamp) { |
| const date = new Date(timestamp * 1000); |
| const year = date.getFullYear(); |
| const month = ('0' (date.getMonth() 1)).slice(-2); |
| const day = ('0' date.getDate()).slice(-2); |
| const hours = ('0' date.getHours()).slice(-2); |
| const minutes = ('0' date.getMinutes()).slice(-2); |
| const seconds = ('0' date.getSeconds()).slice(-2); |
| |
| return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; |
| } |
复制
你可以调用函数 formatDate
并传入一个时间戳作为参数,它将返回类似于 "2022-01-01 12:34:56" 的格式化日期字符串。