一、继承的方法
1、toLocaleString()
new Date().toLocaleString() // '2025/10/17 16:58:27'
2、toString()
new Date().toString() // 'Fri Oct 17 2025 16:58:57 GMT+0800 (中国标准时间)'
3、valueOf()
new Date().valueOf() // 1760691606393
二、格式化方法
new Date().toDateString() // 'Fri Oct 17 2025'
new Date().toTimeString() // '17:02:18 GMT+0800 (中国标准时间)'
new Date().toLocaleDateString() // '2025/10/17'
new Date().toLocaleTimeString() // '17:03:18'
new Date().toUTCString() // 'Fri, 17 Oct 2025 09:03:45 GMT'
三、其他方法
| 方法 | 说明 |
|---|---|
| getTime () | 返回日期的毫秒表示;与 valueof()相同 |
| setTime (milliseconds) | 设置日期的毫秒表示,从而修改整个日期 |
| getFullYear () | 返回4位数年(即2019而不是19) |
| getUTCFullYear () | 返回 UTC 日期的4位数年 |
| setFullYear (year) | 设置日期的年(year 必须是4位数) |
| setUTCFullYear (year) | 设置 UTC日期的年(year 必须是4位数) |
| getMonth ( ) | 返回日期的月(0表示1月,11表示12月) |
| getUTCMonth () | 返回 UTC 日期的月(0表示1月,11表示12月) |
| setMonth (month) | 设置日期的月(month 为大于0的数值,大于11加年) |
| setUTCMonth (month) | 设置 UTC 日期的月(month大于0的数值,大于11加年) |
| getDate () | 返回日期中的日(1~31) |
| getUTCDate () | 返回 UTC 日期中的日(1~31) |
| setDate (date) | 设置日期中的日(如果 date 大于该月天数,则加月) |
| setUTCDate (date) | 设置 UTC 日期中的日(如果 date 大于该月天数,则加月) |
| getDay () | 返回日期中表示周几的数值(0表示周目,6表示周六) |
| getUTCDay () | 返回 UTC 日期中表示周几的数值(0表示周日,6表示周六) |
| getHours () | 返回日期中的时(0~23) |
| getUTCHours () | 返回 UTC日期中的时(0~23) |
| setHours (hours) | 設警日期中的时(如果houte大于23.則加褥 |
| setUTCHours (hours) | 设置 UTC 日期中的时(如果 hours大于23,则加日) |
| getMinutes () | 返回日期中的分(0~59) |
| getUTCMinutes () | 返回 UTC 日期中的分(0~59) |
| setMinutes (minutes) | 设置日期中的分(如果 minutes 大于59,则加时) |
| setUTCMinutes (minutes) | 设置 UTC 日期中的分(如果 minutes 大于59,则加时) |
| getSeconds () | 返回日期中的秒(0~59) |
| getUTCSeconds ( ) | 返回 UTC 日期中的秒(0~59) |
| set Seconds (seconds) | 设置日期中的秒(如果 seconds 大于59,则加分) |
| setUTCSeconds (seconds) | 设置 UTC 日期中的秒(如果 seconds 大于59,则加分) |
| getMilliseconds () | 返回日期中的毫秒 |
| getUTCMilliseconds () | 返回 UTC 日期中的毫秒 |
| setMilliseconds (milliseconds) | 设置日期中的毫秒 |
| setUTCMilliseconds (milliseconds) | 设置 UTC 日期中的毫秒 |
| getTimezoneOffset() | 返回以分钟计的 UTC与本地时区的偏移量(如美国EST即“东部标准时间”返回300,进入夏令时的地区可能有所差异) |
举例说明上诉方法中带UTC方法的区别,带UTC的会早8小时。
new Date(2026,0,1).getFullYear() // 2026
new Date(2026,0,1).getUTCFullYear() // 2025
new Date().getHours() // 17
new Date().getUTCHours() // 9
new Date(2025,10,17,17:10:00).getMinutes() // 10
new Date(2025,10,17,17:10:00).getUTCMinutes() //10