日期对象
掌握 Date 日期对象的使用,动态获取当前计算机的时间。
实例化
// 1. 实例化
// const date = new Date(); // 系统默认时间
const date = new Date('2020-05-01') // 指定时间
// date 变量即所谓的时间对象
console.log(typeof date)
方法
// 1. 实例化
const date = new Date();
// 2. 调用时间对象方法
// 通过方法分别获取年、月、日,时、分、秒
const year = date.getFullYear(); // 四位年份
const month = date.getMonth(); // 0 ~ 11
getFullYear
获取四位年份getMonth
获取月份,取值为 0 ~ 11getDate
获取月份中的每一天,不同月份取值也不相同getDay
获取星期,取值为 0 ~ 6getHours
获取小时,取值为 0 ~ 23getMinutes
获取分钟,取值为 0 ~ 59getSeconds
获取秒,取值为 0 ~ 59
时间戳
时间戳是指1970年01月01日00时00分00秒起始至现在的总秒数或毫秒数,它是一种特殊的计量时间的方式。
// 1. 实例化
const date = new Date()
// 2. 获取时间戳
console.log(date.getTime())
// 还有一种获取时间戳的方法
console.log(+new Date())
// 还有一种获取时间戳的方法
console.log(Date.now())
获取时间戳的方法,分别为getTime
和Date.now
和+new Date()