1 回答
TA贡献1891条经验 获得超3个赞
day如果 day === 1,则不会从该方法返回。因此,day 不会更改。因为你return的 if 语句中有关键字。所以如果 day <= 1,这个代码块将不会因为return关键字而运行。
return { month: prevMonth, year: prevMonthYear }; // This breaks the block.
}
const prevDay = (day < 2) ? prevMonthDays : day - 1; // won't run if day === 1
return { day: prevDay, month, year };
所以,你可以像这样修复它:
export const goPreviousDay = (day, month, year) => {
const prevMonthDays = getMonthDays(month, year);
let newDay, newMonth, newYear;
if (day <= 1) {
newDay = prevMonthDays;
newMonth = (month > 1) ? month - 1 : 12;
newYear = (month > 1) ? year : year - 1;
} else {
newDay = day - 1;
newMonth = month;
newYear = year;
}
return { day: newDay, month: newMonth, year: newYear };
};
goNextDay 方法也是如此。
添加回答
举报