Skip to content

Commit

Permalink
✅ Improved date util (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
jelsabird authored Aug 16, 2023
1 parent a0e3501 commit 78937bf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
51 changes: 42 additions & 9 deletions src/utils/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ test('formatDate works as expected with format = "DD. month" and month = "short"
});

test('formatDateTime works as expected', () => {
const fakeDate = faker.date.past();
const today = new Date();
const fakeDate = faker.date.past({
refDate: today.setDate(today.getDate() - 2),
});
const day = fakeDate.toLocaleDateString('en-GB', { day: 'numeric' });
const expectedResult = `${day}. ${fakeDate.toLocaleString('en-GB', {
month: 'long',
Expand All @@ -172,7 +175,10 @@ test('formatDateTime works as expected when not sending in a date', () => {
});

test('formatDateTime works as expected with options', () => {
const fakeDate = faker.date.past();
const today = new Date();
const fakeDate = faker.date.past({
refDate: today.setDate(today.getDate() - 2),
});
const day = fakeDate.toLocaleDateString('en-GB', { day: 'numeric' });
const expectedResult = `${day}. ${fakeDate.toLocaleString('en-GB', {
month: 'short',
Expand Down Expand Up @@ -260,6 +266,13 @@ test('formatRelativeDateTime with tomorrows date should display as Tomorrow incl

const formattedTomorrow = date.formatRelativeDateTime(tomorrow);
expect(formattedTomorrow).toContain('Tomorrow');

const tomorrowLessThan24Hours = new Date();
tomorrowLessThan24Hours.setDate(tomorrow.getDate());
tomorrowLessThan24Hours.setHours(0);

const formatted = date.formatRelativeDateTime(tomorrowLessThan24Hours);
expect(formatted).toContain('Tomorrow');
});

test('formatRelativeDateTime with date within next week should display as weekday + time', () => {
Expand All @@ -275,17 +288,37 @@ test('formatRelativeDateTime with date within next week should display as weekda
});

test('formatRelativeDateTime with date before yesterday should display as full date not including year', () => {
const threeDaysAgo = new Date();
threeDaysAgo.setDate(threeDaysAgo.getDate() - 3);
const dayNumeric = threeDaysAgo.toLocaleDateString('en-GB', {
day: 'numeric',
});
const expectedResultThreeDaysAgo = `${dayNumeric}. ${threeDaysAgo.toLocaleString(
'en-GB',
{
month: 'long',
hour: '2-digit',
minute: '2-digit',
}
)}`;

const formattedThreeDaysAgo = date.formatRelativeDateTime(threeDaysAgo);
expect(formattedThreeDaysAgo).toBe(expectedResultThreeDaysAgo);

const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
const day = oneWeekAgo.toLocaleDateString('en-GB', { day: 'numeric' });
const expectedResult = `${day}. ${oneWeekAgo.toLocaleString('en-GB', {
month: 'long',
hour: '2-digit',
minute: '2-digit',
})}`;
const expectedResultOneWeekAgo = `${day}. ${oneWeekAgo.toLocaleString(
'en-GB',
{
month: 'long',
hour: '2-digit',
minute: '2-digit',
}
)}`;

const formatted = date.formatRelativeDateTime(oneWeekAgo);
expect(formatted).toBe(expectedResult);
const formattedOneWeekAgo = date.formatRelativeDateTime(oneWeekAgo);
expect(formattedOneWeekAgo).toBe(expectedResultOneWeekAgo);
});

test('formatRelativeDateTime with date after next week displays as full date not including year', () => {
Expand Down
22 changes: 16 additions & 6 deletions src/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,31 @@ const formatRelativeDateTime = (
hour: '2-digit',
minute: '2-digit',
});
if (differenceInDays < 1 && currentDate.getDay() === dateObj.getDay()) {
if (differenceInDays < 1 && dateObj.getDate() === currentDate.getDate()) {
return `Today at ${time}`;
} else if (differenceInDays < 2 && dateObj < currentDate) {
} else if (
differenceInDays < 2 &&
dateObj.getDate() === currentDate.getDate() - 1
) {
// Yesterday
return `Yesterday at ${time}`;
} else if (differenceInDays < 2 && dateObj > currentDate) {
} else if (
differenceInDays < 2 &&
dateObj.getDate() === currentDate.getDate() + 1
) {
// Tomorrow
return `Tomorrow at ${time}`;
} else if (differenceInDays > 2 && differenceInDays < 7) {
// Show day
} else if (
differenceInDays > 2 &&
differenceInDays < 7 &&
dateObj > currentDate
) {
// Show weekday
return `${dateObj.toLocaleString('en-GB', {
weekday: 'long',
})} at ${time}`;
} else {
// More than a week since, show normal formatDateTime
// Before yesterday or in more than a week, show normal formatDateTime
return formatDateTime(date, {
hideYear: dateObj.getFullYear() === currentDate.getFullYear(),
});
Expand Down

0 comments on commit 78937bf

Please sign in to comment.