diff --git a/server/utils/nunjucksSetup.test.ts b/server/utils/nunjucksSetup.test.ts index 8023d614..36bb7125 100644 --- a/server/utils/nunjucksSetup.test.ts +++ b/server/utils/nunjucksSetup.test.ts @@ -131,7 +131,7 @@ describe('Nunjucks Filters', () => { jest.useRealTimers() }) ;[ - { input: '2025-11-15', expected: '' }, // erroneous future date of birth + { input: '3025-11-15', expected: '' }, // future date of birth { input: '2020-11-15', expected: '0 months old' }, { input: '2020-11-14', expected: '1 month old' }, { input: '2020-10-15', expected: '1 month old' }, diff --git a/server/utils/nunjucksSetup.ts b/server/utils/nunjucksSetup.ts index ab529677..f0727b9b 100644 --- a/server/utils/nunjucksSetup.ts +++ b/server/utils/nunjucksSetup.ts @@ -93,22 +93,14 @@ export function registerNunjucks(app?: express.Express): Environment { return '' } - const emptyDuration = { - years: 0, - months: 0, - days: 0, - hours: 0, - minutes: 0, - seconds: 0, - } - const intervalDuration = intervalToDuration({ start: dob, end: today }) - const duration = { ...emptyDuration, ...intervalDuration } + const duration = intervalToDuration({ start: dob, end: today }) let age = '' - if (duration.years < 1) { - age = formatDuration(duration, { format: ['months'], zero: true }) - } else { + if (duration.years) { age = formatDuration(duration, { format: ['years'] }) + } else { + // workaround below for Duration zero/undefined change (https://github.com/date-fns/date-fns/issues/3658) + age = formatDuration(duration, { format: ['months'] }) || '0 months' } return `${age} old`