js怎么获取当前电脑时间
时间: 2023-05-30 08:08:04 浏览: 57
可以使用JavaScript中的Date对象来获取当前电脑时间。
具体步骤如下:
1. 创建一个Date对象,可以不传入任何参数,表示获取当前时间。
```javascript
var now = new Date();
```
2. 获取当前时间的各个部分,如年、月、日、时、分、秒等。
```javascript
var year = now.getFullYear(); //获取年份
var month = now.getMonth() + 1; //获取月份,注意月份从0开始,需要加1
var day = now.getDate(); //获取日期
var hour = now.getHours(); //获取小时
var minute = now.getMinutes(); //获取分钟
var second = now.getSeconds(); //获取秒钟
```
完整的代码如下:
```javascript
var now = new Date();
var year = now.getFullYear(); //获取年份
var month = now.getMonth() + 1; //获取月份,注意月份从0开始,需要加1
var day = now.getDate(); //获取日期
var hour = now.getHours(); //获取小时
var minute = now.getMinutes(); //获取分钟
var second = now.getSeconds(); //获取秒钟
console.log(year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second); //输出当前时间
```
输出结果类似于:2021-1-1 10:30:30