一、实例化日期对象
实例化:带 new 关键字
- 获取当前日期对象
const 变量 = new Date()
- 获取指定日期对象
const 变量 = new Date('时间字符串')
二、时间日期对象的方法
- 创建日期对象
const n = new Date()
- 获取年份:四位数
const YYYY = n.getFullYear()
- 获得月份:取值为 0 ~ 11,0 表示 1 月份,11表示12月份。
const MM = n.getMonth()
- 获取月份中的每一天:1~31之间的一个数字,不同月份取值也不相同。
const DD = n.getDate()
- 获取星期:取值为 0 ~ 6,0 表示星期天。
const week = n.getDay()
- 获取小时:取值为 0 ~ 23
const hh = n.getHours()
- 获取分钟:取值为 0 ~ 59
const mm = n.getMinutes()
- 获取秒:取值为 0 ~ 59
const ss = n.getSeconds()
三、时间戳
时间戳:一种特殊的计量时间的方式,指1970年01月01日00时00分00秒起至现在的毫秒数。
时间戳使用场景:计算倒计时
- 获取现在的时间戳
console.log(+new Date())
- 获取指定时间的时间戳
console.log(+new Date('2022-12-1 18:30:00'))