1. 时间戳
时间戳是格林威治时间1970年1月1日0时0分0秒到现在的总秒数,如果得到毫秒数需要/1000。** 时间戳用于前后端交互。拿到时间戳将一串数字转化为对应的时间。**
2. Date构造函数
// 打印当前时间
var today = new Date();
console.log(today);//Fri Feb 25 2021 17:31:33 GMT+0800 (中国标准时间)
作为构造函数时,可以传各种格式参数,返回该参数对应的时间
// 1970年1月1日0时0分0秒到某一时刻的总毫秒数163542577689
var time1 = new Date(163542577689);
var time2 = new Date("January 6,2010");
var time3 = new Date(2020,0,1,0,0,0,0);
var time4 = new Date("2020-1-1");
console.log(time1); // Sun Mar 09 1975 04:29:37 GMT+0800 (中国标准时间)
console.log(time2); // Wed Jan 06 2010 00:00:00 GMT+0800 (中国标准时间)
console.log(time3); // Wed Jan 01 2020 00:00:00 GMT+0800 (中国标准时间)
console.log(time4); // Wed Jan 01 2020 00:00:00 GMT+0800 (中国标准时间)