- 注意点: 在创建指定时间的时候, 如果月份是单独传入的, 那么会多一个月
1 | let date2 = new Date(2019, 10, 11, 9, 8, 7); |
- 注意点; 通过getMonth方法获取到的月份会少一个月
1 | let date2 = new Date(2019, 10, 11, 9, 8, 7); |
1 | 1.获取当前时间 |
- 时间格式化
1
2
3
4
5
6
7
8
9
let date = new Date();
let res = formartDate(date);
console.log(res);
// console.log(date);
// 2019-4-19 18:17:06
function formartDate(date) {
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
}