js字符串转年月日时分秒
时间: 2023-11-11 13:58:44 浏览: 113
js中获取年月日时分秒
可以使用JavaScript内置的Date对象来实现字符串转年月日时分秒的功能。具体实现方法如下:
```javascript
const dateString = '2021-08-31 12:30:45';
const date = new Date(dateString);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
console.log(`${year}-${month}-${day} ${hour}:${minute}:${second}`);
```
输出结果为:`2021-8-31 12:30:45`
阅读全文