Skip to content

Date

创建日期对象

js
const date = new Date();

常用方法

方法描述示例
new Date()获取日期对象Wed Jul 31 2024 06:40:04 GMT+0000 (Coordinated Universal Time)
getFullYear()获取四位数的年份2024
getMonth()获取月份(0-11,0代表一月,11代表十二月)6
getDate()获取月份中的日(1-31)31
getDay()获取星期几(0-6,0代表星期日,6代表星期六)3
getHours()获取小时数(0-23)6
getMinutes()获取分钟数(0-59)40
getSeconds()获取秒数(0-59)4
getMilliseconds()获取毫秒数(0-999)740
getTime()获取自1970年1月1日以来的毫秒数1722408004740
Date.now()返回当前时间的毫秒数1722408004740
toDateString()将日期部分转换为易读格式,不包括时间Wed Jul 31 2024
toTimeString()将时间部分转换为易读格式06:40:04 GMT+0000 (Coordinated Universal Time)
toLocaleDateString()根据本地时间格式,返回日期部分的字符串7/31/2024
toLocaleTimeString()根据本地时间格式,返回时间部分的字符串6:40:04 AM
toLocaleString()根据本地时间格式,返回日期和时间的字符串7/31/2024, 6:40:04 AM
toISOString()以ISO格式返回字符串2024-07-31T06:40:04.740Z
toJSON()以JSON格式返回日期字符串2024-07-31T06:40:04.740Z
toString()将日期转换为完整的字符串表示Wed Jul 31 2024 06:40:04 GMT+0000 (Coordinated Universal Time)
toUTCString()将日期转换为UTC格式的字符串Wed, 31 Jul 2024 06:40:04 GMT
valueOf()返回日期对象的原始值,即自1970年1月1日以来的毫秒数1722408004740