function getLastDayOfMonth(year, month) {
var date= new Date(year, month + 2, 0);
date.setDate(0);
date=date.toLocaleDateString();
return date;
}
const months = ["20221110","20221221","20230101","20230201","20230301"];
for (const dateStr of months) {
const year = parseInt(dateStr.substring(0, 4));
const month = parseInt(dateStr.substring(4, 6)) - 1; // 月份从 0 开始
const lastDay = getLastDayOfMonth(year, month);
console.log(lastDay);
}
// 运行结果
// 2022/10/31
// 2022/11/30
// 2022/12/31
// 2023/1/31
// 2023/2/28