|
1 | 1 | export const dateToString = (published: Date | string) =>
|
2 | 2 | new Date(published).toISOString().split("T")[0]
|
3 |
| - |
4 |
| -export const formatDateRange = (startDate: string, endDate: string) => { |
5 |
| - // Parse the input dates |
6 |
| - // .replace(/-/g, "/") ==> Fixes Safari Invalid date |
7 |
| - |
8 |
| - const start = new Date(startDate.replace(/-/g, "/")) |
9 |
| - const end = new Date(endDate.replace(/-/g, "/")) |
10 |
| - |
11 |
| - // Formatter for day and month |
12 |
| - // undefined :: denotes to use automatic user's locale |
13 |
| - const dayMonthFormatter = new Intl.DateTimeFormat(undefined, { |
14 |
| - day: "numeric", |
15 |
| - month: "short", |
16 |
| - }) |
17 |
| - |
18 |
| - // Extract formatted strings |
19 |
| - const startDayMonth = dayMonthFormatter.format(start) |
20 |
| - const endDayMonth = dayMonthFormatter.format(end) |
21 |
| - const startMonth = new Intl.DateTimeFormat(undefined, { |
22 |
| - month: "short", |
23 |
| - }).format(start) |
24 |
| - const endMonth = new Intl.DateTimeFormat(undefined, { |
25 |
| - month: "short", |
26 |
| - }).format(end) |
27 |
| - |
28 |
| - // Determine the date range string |
29 |
| - let dateRangeString |
30 |
| - if (start.toDateString() === end.toDateString()) { |
31 |
| - dateRangeString = startDayMonth // If the start and end dates are the same |
32 |
| - } else if ( |
33 |
| - startMonth === endMonth && |
34 |
| - start.getFullYear() === end.getFullYear() |
35 |
| - ) { |
36 |
| - // Output as "12-14 Jul" in the user's locale format |
37 |
| - dateRangeString = `${start.getDate()}-${end.getDate()} ${startMonth}` |
38 |
| - } else { |
39 |
| - // If different months or years, show as "12 Jul - 14 Aug" |
40 |
| - dateRangeString = `${startDayMonth}-${endDayMonth}` |
41 |
| - } |
42 |
| - |
43 |
| - return dateRangeString |
44 |
| -} |
0 commit comments