Skip to content

Commit

Permalink
Add custom separator support
Browse files Browse the repository at this point in the history
  • Loading branch information
timolins committed Feb 2, 2024
1 parent 72e4eac commit 1fa0f0e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ formatDateRange(from, to, {
locale: "de-AT", // Overwrite the default locale
includeTime: false, // Prevent time from being displayed
today: new Date(), // Overwrite the default "today" date, useful for testing
separator: "-", // Overwrite the default separator. E.g. from "Jan 1 - 12" to "Jan 1 to 12"
});
```

Expand Down
12 changes: 12 additions & 0 deletions src/format-date-range.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ describe("format date range", () => {
expect(formatDateRange(from, to, defaultOptions)).toBe("Jan - Feb 2023");
});

test("custom separator", () => {
const from = new Date("2023-01-01T00:00:00.000Z");
const to = new Date("2023-01-12T23:59:59.999Z");

expect(
formatDateRange(from, to, {
...defaultOptions,
separator: "to",
})
).toBe("Jan 1 to 12");
});

test("automatic locale detection should not fail", () => {
const from = new Date("2023-01-01T00:00:00.000Z");
const to = new Date("2023-01-12T23:59:59.999Z");
Expand Down
34 changes: 21 additions & 13 deletions src/format-date-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface DateRangeFormatOptions {
today?: Date;
locale?: string;
includeTime?: boolean;
separator?: string;
}

export const formatDateRange = (
Expand All @@ -61,14 +62,14 @@ export const formatDateRange = (
today = new Date(),
locale = getNavigatorLanguage(),
includeTime = true,
separator = "-",
}: DateRangeFormatOptions = {}
): string => {
const sameYear = isSameYear(from, to);
const sameMonth = isSameMonth(from, to);
const sameDay = isSameDay(from, to);
const thisYear = isSameYear(from, today);
const thisDay = isSameDay(from, today);
const toDateIsNow = isSameMinute(to, today);

const yearSuffix = thisYear ? "" : `, ${format(to, "yyyy")}`;

Expand Down Expand Up @@ -111,13 +112,16 @@ export const formatDateRange = (
return `${format(from, "LLLL yyyy")}`;
}
// Example: Jan - Feb 2023
return `${format(from, "LLL")} - ${format(to, "LLL yyyy")}`;
return `${format(from, "LLL")} ${separator} ${format(to, "LLL yyyy")}`;
}

// Range across years
// Example: Jan 1 '23 - Feb 12 '24
if (!sameYear) {
return `${format(from, "LLL d ''yy")}${startTimeSuffix} - ${format(
return `${format(
from,
"LLL d ''yy"
)}${startTimeSuffix} ${separator} ${format(
to,
"LLL d ''yy"
)}${endTimeSuffix}`;
Expand All @@ -126,7 +130,7 @@ export const formatDateRange = (
// Range across months
// Example: Jan 1 - Feb 12[, 2023]
if (!sameMonth) {
return `${format(from, "LLL d")}${startTimeSuffix} - ${format(
return `${format(from, "LLL d")}${startTimeSuffix} ${separator} ${format(
to,
"LLL d"
)}${endTimeSuffix}${yearSuffix}`;
Expand All @@ -137,29 +141,33 @@ export const formatDateRange = (
// Check for a time suffix, if so print the month twice
// Example: Jan 1, 12:00pm - Jan 2, 1:00pm[, 2023]
if (startTimeSuffix || endTimeSuffix) {
return `${format(from, "LLL d")}${startTimeSuffix} - ${format(
return `${format(from, "LLL d")}${startTimeSuffix} ${separator} ${format(
to,
"LLL d"
)}${endTimeSuffix}${yearSuffix}`;
}

// Example: Jan 1 - 12[, 2023]
return `${format(from, "LLL d")} - ${format(to, "d")}${yearSuffix}`;
return `${format(from, "LLL d")} ${separator} ${format(
to,
"d"
)}${yearSuffix}`;
}

// Same day, different times
// Example: Jan 1, 12:00pm - 1:00pm[, 2023]
// Example: Jan 1, 12pm - 1pm[, 2023]
if (startTimeSuffix || endTimeSuffix) {
// If it's today, don't include the date
// Example: 12:00pm - 1:00pm
// Example: 12:30pm - 1pm
if (thisDay) {
return `${formatTime(from)} - ${formatTime(to)}`;
return `${formatTime(from)} ${separator} ${formatTime(to)}`;
}

// Example: Jan 1, 12:00pm - 1:00pm[, 2023]
return `${format(from, "LLL d")}${startTimeSuffix} - ${formatTime(
to
)}${yearSuffix}`;
// Example: Jan 1, 12pm - 1pm[, 2023]
return `${format(
from,
"LLL d"
)}${startTimeSuffix} ${separator} ${formatTime(to)}${yearSuffix}`;
}

// Full day
Expand Down

0 comments on commit 1fa0f0e

Please sign in to comment.