Skip to content

Date

创建日期对象

js
const date = new Date();

常用方法

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