Date 对象用于处理日期和时间
序号 | 方法 | 描述 |
---|---|---|
1 | new Date() | 获取当前时间 |
2 | parse() | 返回毫秒数(从 1970-1-1 00:00:00开始) |
3 | getDate() | 返回月中的第几天(从 1 到 31) |
4 | getFullYear() | 返回年份 |
5 | getMonth() | 返回月份(从 0-11) |
6 | getDay() | 返回星期几(0-6)0 表示星期天 |
7 | getHours() | 返回小时(从 0-23) |
8 | getMinutes() | 返回分钟(从 0-59) |
9 | toISOString() | 返回一个 ISO格式的字符串:YYYY-MM-DDTHH:mm:ss.sssZ |
10 | toUTCString() | 把一个日期转换为一个字符串,使用 UTC 时区 |
11 | toString() | 返回一个字符串,以本地的时区表示 |
12 | toJSON() | 返回 Date 对象的字符串形式 |
13 | toDateString() | 返回日期部分的字符串 |
14 | toTimeString() | 返回时间部分的字符串 |
代码部分
let date = new Date()
console.log(date)// Fri Mar 22 2024 21:30:51 GMT+0800 (中国标准时间)
console.log(Date.parse(date))// 1711114251000
console.log(date.getDate())// 22
console.log(date.getFullYear())// 2024
console.log(date.getMonth())// 2
console.log(date.getDay())// 5
console.log(date.getHours())// 21
console.log(date.getMinutes())// 30
console.log(date.toISOString())// 2024-03-22T13:30:51.796Z
console.log(date.toUTCString())// Fri, 22 Mar 2024 13:30:51 GMT
console.log(date.toString())// Fri Mar 22 2024 21:30:51 GMT+0800 (中国标准时间)
console.log(date.toJSON())// 2024-03-22T13:30:51.796Z
console.log(date.toDateString())// Fri Mar 22 2024
console.log(date.toTimeString())// 21:30:51 GMT+0800 (中国标准时间)