// 日期对象是内置对象
console.log(new Date()) // Tue Jun 02 2020 12:37:37 GMT+0800 (中国标准时间)
console.log(typeof (new Date())) // object
// 使用函数获取时间
console.log(Date()) // Tue Jun 02 2020 12:37:37 GMT+0800 (中国标准时间)
// 获取时间戳
console.log(Date.now()) //1591072805971
console.log((new Date()).getTime());
console.log((new Date()).valueOf());
console.log(Number(new Date())) // 直接转换为整数
// 计算执行时间
let start = Date.now();
for (let i = 0; i < 200000000; i++) { }
let end = Date.now();
console.log(`执行时间为:${end - start}`);
// 指定日期和时间对象
// 使用字符串表示时间
let now1 = '2020-05-22 12:43:12'
let now = new Date(now1);
console.log(now) // Fri May 22 2020 12:43:12 GMT+0800 (中国标准时间)
// 使用数组表示时间
let now2 = [2020, 1, 22, 12, 44]; // 月份从0-11
console.log(new Date(...now2)) // Sat Feb 22 2020 12:44:00 GMT+0800 (中国标准时间)
// 日期类库
// http://momentjs.cn/
一键复制
编辑
Web IDE
原始数据
按行查看
历史