diff --git a/packages/frontend/i18n/src/utils/__tests__/time.spec.ts b/packages/frontend/i18n/src/utils/__tests__/time.spec.ts index 651d44625b..44321a4b18 100644 --- a/packages/frontend/i18n/src/utils/__tests__/time.spec.ts +++ b/packages/frontend/i18n/src/utils/__tests__/time.spec.ts @@ -396,4 +396,9 @@ describe('humanTime', () => { }) ).toBe('本月'); }); + + test('invalid time', () => { + createI18n(); + expect(i18nTime('foobar')).toBe(''); + }); }); diff --git a/packages/frontend/i18n/src/utils/time.ts b/packages/frontend/i18n/src/utils/time.ts index 4d7e015471..a1e3bae92e 100644 --- a/packages/frontend/i18n/src/utils/time.ts +++ b/packages/frontend/i18n/src/utils/time.ts @@ -73,6 +73,10 @@ export function i18nTime( ) { const i18n = options.i18n ?? I18n; time = dayjs(time); + if (!time.isValid()) { + return ''; // invalid time, return empty string + } + const now = dayjs(options.now); const defaultRelativeOption = {