日期对象
作用:用来表示时间的对象
获取当前时间
const date=new Date();console.log(date);
可以得到日期对象,里面的属性有星期,年月日,时分秒
![]()
获取指定时间
const date=new Date('2023-05-01');console.log(date);
获取时间戳
时间戳是自从1970年1月1日的午夜开始到现在流过的毫秒数
const date=+new Date();console.log(date);
日期对象方法
| 方法 | 作用 |
|---|---|
| getFullYear() | 获取四位数年份 |
| getMonth() | 获得月份,取值为0~11 |
| getDate() | 获取月份中的天数,一个月有多少天 |
| getDay() | 获取星期,取值为0~6 |
| getHours() | 获取小时,取值为0~23 |
| getMinutes() | 获取分数,取值为0~59 |
| getSeconds() | 获取秒,取值为0~59 |