js 获取当前时间 格式为 2022-12-24 13:25:59
时间: 2023-05-11 15:07:37 浏览: 147
你可以使用 JavaScript 的 `Date` 对象来获取当前时间。
例如,下面的代码将会在控制台中输出当前时间:
```
var date = new Date();
console.log(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
```
这段代码会输出类似这样的时间格式:"2022-12-24 13:25:59"。
注意,获取的月份是从 0 开始的,所以需要加 1 才能得到正确的月份。
相关问题
js 获取当前时间格式为2022-12-24 13:25:59
你可以使用下面的代码来获取当前时间并格式化为2022-12-24 13:25:59的格式:
```javascript
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
month = month < 10 ? '0' + month : month;
var day = now.getDate();
day = day < 10 ? '0' + day : day;
var hour = now.getHours();
hour = hour < 10 ? '0' + hour : hour;
var minute = now.getMinutes();
minute = minute < 10 ? '0' + minute : minute;
var second = now.getSeconds();
second = second < 10 ? '0' + second : second;
var currentTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
console.log(currentTime);
```
请注意,由于我是一个AI语言模型,我不支持直接调用JS代码。如果您需要可编程的交互,请参考CSDN的开发API。
js获取当前时间格式为(YYYY-MM-DD 24hh:mm:ss
要获取当前时间并格式化为"YYYY-MM-DD 24hh:mm:ss"的格式,可以使用JavaScript的Date对象和相关方法来实现。下面是一种实现方式:
```javascript
// 创建一个Date对象
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 formattedDate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
console.log(formattedDate); // 输出格式化后的时间字符串
```
这段代码首先创建了一个Date对象,然后使用Date对象的方法获取当前的年、月、日、小时、分钟和秒。接着使用字符串拼接的方式将这些值组合成指定的格式,并将结果打印出来。