一,获取日期
- getFullYear() - 根据本地时间,返回指定日期的年
- getMonth() - 根据本地时间,返回指定日期的月(从0开始算,依次加1月)
- getDate() - 根据本地时间,返回指定日期的日
- getHours() - 根据本地时间,返回指定日期的小时
- getMinutes() - 根据本地时间,返回指定日期的分钟
- getSeconds() - 根据本地时间,返回指定日期的秒
- getMilliseconds() - 根据本地时间,返回指定日期的毫秒
- getDay() - 根据本地时间,返回指定日期的一周中的第几天(0表示周天)
- getTime() - 返回指定日期的时间戳(当前时间距1970年1月1日 00:00:00 UTC到当前时间的毫秒数)
- Date.now() - 与getTime一致
二,设置日期
对应获取日期的方法,例如
- setFullYear(val)
- setMonth(val)
- ... ...
三,日期转换字符串
- toJSON() - 返回Data对象的字符串形式
- toString() - 以本地的时区转换Date为字符串形式(eg: 'Wed May 17 2023 11:10:04 GMT+0800 (中国标准时间)')
- toLocaleString() - 以日期的格式转换Date为字符串形式(eg: '2023/5/17 11:11:23')
-
toISOString() - 以UTC时区,转换为ISO格式的字符串(YYYY-MM-DDTHH:mm:ss.sssZ)
其他详细方法查看API
MDNhttps://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date