js获取当前时间时分秒
时间: 2023-10-21 13:33:16 浏览: 82
以下是JavaScript代码,可以获取当前时间的时分秒:
var currentDate = new Date(); //获取当前时间
var hours = currentDate.getHours(); //获取小时
var minutes = currentDate.getMinutes(); //获取分钟
var seconds = currentDate.getSeconds(); //获取秒钟
console.log(hours + ":" + minutes + ":" + seconds); //输出当前时间的时分秒
相关问题
js 获取当前时间 时分秒
以下是获取当前时间的时分秒的JavaScript代码:
```javascript
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
console.log(hour + ":" + minute + ":" + second);
```
这段代码首先创建了一个Date对象,然后使用getHours()、getMinutes()和getSeconds()方法获取当前时间的小时、分钟和秒数,最后将它们拼接成一个字符串并输出到控制台上。
js获取当前时间时分秒时间戳
可以使用JavaScript中的Date对象来获取当前时间的时分秒时间戳,具体代码如下:
```
var now = new Date();
var timestamp = now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds();
console.log(timestamp);
```