我有一个调用的按钮goNextDay和另一个调用goPreviousDay. 两者都转到正确的月份,但prevMonthDays如果天 === 1,则不会将日期设置为上个月,如果天 === thisMonthDays,则将下个月的日期设置为 1。
这是与下面相同的代码,但有任何即将进行的改进。
转到 src>UIConainers>Calendar>CalSlideDrawer.js
https://codesandbox.io/s/zen-dijkstra-1c31n?fontsize=14
CalSlideDrawer.js 是通过左上角的标志图标找到的(如果您在紫色屏幕上开始,则按下收件箱图标后)
export const getMonthDays = (month, year) => {
const months30 = [4, 6, 9, 11];
const leapYear = year % 4 === 0;
return month === 2
? leapYear
? 29
: 28
: months30.includes(month)
? 30
: 31;
};
export const goPreviousDay = (day, month, year) => {
const prevMonthDays = getMonthDays(month, year);
if (day <= 1) {
const prevMonth = (month > 1) ? month - 1 : 12;
const prevMonthYear = (month > 1) ? year : year - 1;
return { month: prevMonth, year: prevMonthYear };
}
const prevDay = (day < 2) ? prevMonthDays : day - 1;
return { day: prevDay, month, year };
};
export const goNextDay = (day, month, year) => {
const thisMonthDays = getMonthDays(month, year);
if (day === thisMonthDays) {
const nextMonth = month < 12 ? month + 1 : 1;
const nextMonthYear = month < 12 ? year : year + 1;
return { month: nextMonth, year: nextMonthYear };
}
const nextDay = day === thisMonthDays ? 1: day + 1;
return { day: nextDay, month, year };
};
谢谢阅读。你能说出我的逻辑有什么问题吗?请告知,非常感谢
万千封印
相关分类