Replace isRelativeDay with date-fns isYesterday (#8286)

* Fix isRelativeDay returning wrong result across month boundaries

isRelativeDay compared the target date's day-of-month against the raw
`getDate() + offsetInDays` value instead of the day after setDate()
normalizes it. On the 1st of a month that raw value is 0 (or negative),
so a date that really is "yesterday" (the last day of the previous month)
was never matched. This made the "Yesterday" grouping in the Upcoming
Episodes view fall back to a full date on the 1st of every month.

Compare against yesterday.getDate() after the setDate() adjustment so the
month/year rollover is respected. Add unit tests covering the same-month,
month-boundary and year-boundary cases for -1/0/+1 offsets.

* Replace isRelativeDay with date-fns isYesterday

Both call sites only ever asked for an offset of -1, so drop the custom
helper and use date-fns instead, per review feedback.

---------

Co-authored-by: Konstantin Ostrovskii <4070730+Grinv@users.noreply.github.com>
This commit is contained in:
Konstantin OstrovskiiandKonstantin Ostrovskii authored and GitHub committed 2026-10-21 13:42:06 -04:00
1 parent 65bd97aa5e
commit 17aae059c3
3 files changed
+6 -16

No files matched your search

@@ -1,3 +1,5 @@
import { isYesterday } from 'date-fns';
import cardBuilder from 'components/cardbuilder/cardBuilder';
import { getBackdropShape } from 'components/cardbuilder/utils/shape';
import imageLoader from 'components/images/imageLoader';
@@ -55,7 +57,7 @@ function renderUpcoming(elem, items) {
if (item.PremiereDate) {
try {
const premiereDate = datetime.parseISO8601Date(item.PremiereDate, true);
dateText = datetime.isRelativeDay(premiereDate, -1) ? globalize.translate('Yesterday') : datetime.toLocaleDateString(premiereDate, {
dateText = isYesterday(premiereDate) ? globalize.translate('Yesterday') : datetime.toLocaleDateString(premiereDate, {
weekday: 'long',
month: 'short',
day: 'numeric'
@@ -1,3 +1,5 @@
import { isYesterday } from 'date-fns';
import datetime from 'scripts/datetime';
import globalize from 'lib/globalize';
import type { ItemDto } from 'types/base/models/item-dto';
@@ -22,7 +24,7 @@ export function groupsUpcomingEpisodes(items: ItemDto[]) {
item.PremiereDate,
true
);
dateText = datetime.isRelativeDay(premiereDate, -1) ?
dateText = isYesterday(premiereDate) ?
globalize.translate('Yesterday') :
datetime.toLocaleDateString(premiereDate, {
weekday: 'long',
-14
View File
@@ -273,19 +273,6 @@ export function getDisplayTime(date) {
return time;
}
export function isRelativeDay(date, offsetInDays) {
if (!date) {
throw new Error('date cannot be null');
}
const yesterday = new Date();
const day = yesterday.getDate() + offsetInDays;
yesterday.setDate(day); // automatically adjusts month/year appropriately
return date.getFullYear() === yesterday.getFullYear() && date.getMonth() === yesterday.getMonth() && date.getDate() === day;
}
export default {
parseISO8601Date: parseISO8601Date,
getDisplayRunningTime: getDisplayRunningTime,
@@ -293,7 +280,6 @@ export default {
toLocaleDateString: toLocaleDateString,
toLocaleString: toLocaleString,
getDisplayTime: getDisplayTime,
isRelativeDay: isRelativeDay,
toLocaleTimeString: toLocaleTimeString,
supportsLocalization: function () {
return toLocaleTimeStringSupportsLocales;