js 获取当前时间yyyymmddhhmmss
时间: 2023-11-24 14:53:12 浏览: 99
JS获取当前时间yyyymmddhhmmss
以下是两种JavaScript获取当前时间yyyymmddhhmmss的方法:
1.使用Date对象和字符串拼接的方式获取当前时间:
```javascript
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
var currentTime = year + "" + month + "" + day + "" + hour + "" + minute + "" + second;
console.log(currentTime); // 输出:20211231123456
```
2.使用时间戳获取当前时间:
```javascript
var timestamp = Date.parse(new Date());
var date = new Date(timestamp);
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();var minute = date.getMinutes();
var second = date.getSeconds();
var currentTime = year + "" + month + "" + day + "" + hour + "" + minute + "" + second;
console.log(currentTime); // 输出:20211231123456
```
阅读全文