js如何获得yyyy-mm-dd hh:mm:ss 格式时间戳
时间: 2023-09-24 20:12:34 浏览: 57
你可以使用JavaScript中的`Date`对象来获取当前时间,然后使用一些方法来格式化时间戳为"yyyy-mm-dd hh:mm:ss"的格式。下面是一个示例代码:
```javascript
// 获取当前时间
var currentDate = new Date();
// 获取年、月、日、小时、分钟、秒
var year = currentDate.getFullYear();
var month = ("0" + (currentDate.getMonth() + 1)).slice(-2);
var day = ("0" + currentDate.getDate()).slice(-2);
var hours = ("0" + currentDate.getHours()).slice(-2);
var minutes = ("0" + currentDate.getMinutes()).slice(-2);
var seconds = ("0" + currentDate.getSeconds()).slice(-2);
// 拼接时间戳
var timestamp = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
console.log(timestamp); // 输出示例:2022-01-01 12:34:56
```
这段代码将获得当前时间,并将其格式化为"yyyy-mm-dd hh:mm:ss"的时间戳格式。你可以根据需要进行修改和调整。