创建日期对象
const date = new Date();console.log(date);// node:2022-01-20T06:18:37.876Z (时区问题:默认的是格林尼治时间)// 浏览器:Thu Jan 20 2022 14:16:32 GMT+0800 (中国标准时间)var birthday = new Date("1999-12-24 12:24:48");console.log(birthday);var birthday = new Date(1999, 11, 24, 12, 24, 48);console.log(birthday);/** 1999-12-24T04:24:48.000Z* Fri Dec 24 1999 12:24:48 GMT+0800 (中国标准时间)*/
- 没有参数,返回系统当前时间
方法
获取年月日时分秒星期
const birthday = new Date("1999-12-24 12:24:48");console.log(birthday);console.log(birthday.getFullYear()); // 1999 (当年)console.log(birthday.getMonth()); // 11 (当月0~11)console.log(birthday.getDate()); // 24 (当天日期)console.log(birthday.getDay()); // 5 (星期0~6)console.log(birthday.getHours()); // 12 (当前小时)console.log(birthday.getMinutes()); // 24 (当前分钟)console.log(birthday.getSeconds()); // 48 (当前秒钟)
获取总毫秒时间(时间戳)
const date = new Date();console.log(date.valueOf());console.log(date.getTime());const time = +new Date();console.log(time);console.log(Date.now());
Date 是基于 1970 年 1 月 1 日 (世界标准时间) 开始计算的毫秒数,一直计算到当前日期的毫秒时间
