写一个函数 "2022-03-01" 返回 3月的最后一天
function getLastDayOfMonth(dateString) {
const date = new Date(dateString);
const year = date.getFullYear();
const month = date.getMonth() + 1; // 注意:月份从0开始,所以要加1
const lastDay = new Date(year, month, 0).getDate();
// 格式化最后一天的日期
const lastDayString = `${year}-${month.toString().padStart(2, '0')}-${lastDay.toString().padStart(2, '0')}`;
return lastDayString;
}
// 示例用法
const date = "2022-03-01";
const lastDay = getLastDayOfMonth(date);
console.log(lastDay); // 输出 "2022-03-31"