diff --git a/lib/dateTime.js b/lib/dateTime.js index 6ac5812..c8c0664 100644 --- a/lib/dateTime.js +++ b/lib/dateTime.js @@ -1198,21 +1198,6 @@ export function formatRelativeDateTime(then) { let numeric = 'always'; const thenTS = then.getTime(); - if (unit === 'day' || unit === 'hour' && Math.round(Math.abs(value)) >= 6) { - - const fullDay = 24 * 60 * 60 * 1000; - const today = new Date(now.toDateString()).getTime(); - - const yesterday = thenTS < today && thenTS > (today - fullDay); - const tomorrow = thenTS >= today + fullDay && thenTS < (today + fullDay * 2); - - if (yesterday || tomorrow) { - numeric = 'auto'; - unit = 'day'; - value = Math.sign(value); - } - } - if (unit === 'week' || unit === 'day' && Math.round(Math.abs(value)) >= 4) { const fullWeek = 7 * 24 * 60 * 60 * 1000; @@ -1222,7 +1207,7 @@ export function formatRelativeDateTime(then) { return d.getTime(); })(new Date(now)); - const lastWeek = thenTS < thisWeek && thenTS > (thisWeek - fullWeek); + const lastWeek = thenTS < thisWeek && thenTS >= (thisWeek - fullWeek); const nextWeek = thenTS >= thisWeek + fullWeek && thenTS < (thisWeek + fullWeek * 2); if (lastWeek || nextWeek) { @@ -1231,6 +1216,20 @@ export function formatRelativeDateTime(then) { value = Math.sign(value); } } + else if (unit === 'day' || unit === 'hour' && Math.round(Math.abs(value)) >= 6) { + + const fullDay = 24 * 60 * 60 * 1000; + const today = new Date(now.toDateString()).getTime(); + + const yesterday = thenTS < today && thenTS >= (today - fullDay); + const tomorrow = thenTS >= today + fullDay && thenTS < (today + fullDay * 2); + + if (yesterday || tomorrow) { + numeric = 'auto'; + unit = 'day'; + value = Math.sign(value); + } + } const rtf = new Intl.RelativeTimeFormat(getLanguage(), { localeMatcher: 'best fit', diff --git a/test/dateTime.js b/test/dateTime.js index 0a25192..e712629 100644 --- a/test/dateTime.js +++ b/test/dateTime.js @@ -1034,10 +1034,14 @@ describe('dateTime', () => { [hoursAgo(5.49), '5 hours ago'], [hoursAgo(5.51), 'yesterday'], [hoursAgo(23.51), 'yesterday'], + [new Date('12/31/2022, 12:00:00:000 AM'), 'yesterday'], + [new Date('12/30/2022, 11:59:59:999 PM'), '1 day ago'], [daysAgo(1.49), '1 day ago'], [daysAgo(1.51), '2 days ago'], [daysAgo(3.51), 'last week'], [daysAgo(6.51), 'last week'], + [new Date('12/25/2022, 12:00:00:000 AM'), 'last week'], + [new Date('12/24/2022, 11:59:59:999 PM'), '1 week ago'], [weeksAgo(1.49), '1 week ago'], [weeksAgo(1.51), '2 weeks ago'], [weeksAgo(3.49), '3 weeks ago'],