Fix(Fortune): Calculate the time difference for events that have already occurred based on the next year

For example, if today is January 2nd, then the New Year's Day on January 1st should be calculated using January 1st of the next year.
This commit is contained in:
2024-12-25 11:35:26 +08:00
parent 7852572f26
commit e80f077ae3

View File

@@ -133,12 +133,14 @@ function daysDiff(eventIndex) {
}
const triggerDate = event.triggerDate;
let isCustomEvent = false;
eventYear = year;
if ('year' in triggerDate) {
eventYear = validateNumber(triggerDate.year, 1, maxDate.getFullYear(), 'triggerDate.year', event);
if (eventYear === null) {
return -1;
}
isCustomEvent = true;
}
if (!('month' in triggerDate)) {
@@ -179,12 +181,17 @@ function daysDiff(eventIndex) {
eventDate = firstTargetDay + (triggerDate.week - 1) * 7;
}
if (!isCustomEvent && (month > eventMonth || (month == eventMonth && date > eventDate))) {
eventYear += 1;
}
const endDate = new Date(
eventYear,
eventMonth - 1,
eventDate,
);
// calculate the difference in milliseconds and convert it to days
const timeDiff = Math.ceil((endDate - startDate) / (1000 * 60 * 60 * 24));
return timeDiff;