返回给定日期月份中最后一个日期的字符串表示形式。
- 使用
Date.prototype.getFullYear()
,Date.prototype.getMonth()
从给定日期获取当前年份和月份。 - 使用
Date
构造函数创建一个新的日期,给定的年份和月份增加1
,并将日期设置为0
(上个月的最后一天)。 - 省略参数 ,
date
以默认使用当前日期。const lastDateOfMonth = (date = new Date()) => { let d = new Date(date.getFullYear(), date.getMonth() + 1, 0); return d.toISOString().split('T')[0]; }; lastDateOfMonth(new Date('2015-08-11')); // '2015-08-30'