将当前时间格式化为“2019-09-01 14:29:00”,请用JavaScript代码实现
时间: 2023-03-13 21:19:24 浏览: 52
使用 JavaScript 代码格式化时间,可以使用以下语句:let date = new Date();
let formattedDate = date.toISOString().slice(0, 19).replace('T', ' ');console.log(formattedDate); // 2019-09-01 14:29:00
相关问题
将当前时间格式化为“2019-09-01 14:29:00”,请用最新JavaScript代码实现
答:let currentTime = new Date(); let formattedTime = currentTime.toISOString().replace(/T/, ' ').replace(/\..+/, ''); console.log(formattedTime); // 2019-09-01 14:29:00
vue获取当前实际时间,格式为2022-01-01 10:10:10
你可以使用JavaScript内置的Date对象来获取当前时间,然后使用toLocaleString()方法将时间格式化为所需的字符串格式。以下是一个示例代码:
```
let now = new Date();
let year = now.getFullYear();
let month = now.getMonth() + 1;
let day = now.getDate();
let hours = now.getHours();
let minutes = now.getMinutes();
let seconds = now.getSeconds();
let currentTime = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')} ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
console.log(currentTime);
```
这将输出当前时间的字符串表示形式,格式为2022-01-01 10:10:10。