|
1 | 1 | export const dateToString = (published: Date | string) =>
|
2 | 2 | new Date(published).toISOString().split("T")[0]
|
3 | 3 |
|
4 |
| -// Define an array of month names |
5 |
| -export const months = [ |
6 |
| - "Jan", |
7 |
| - "Feb", |
8 |
| - "Mar", |
9 |
| - "Apr", |
10 |
| - "May", |
11 |
| - "Jun", |
12 |
| - "Jul", |
13 |
| - "Aug", |
14 |
| - "Sep", |
15 |
| - "Oct", |
16 |
| - "Nov", |
17 |
| - "Dec", |
18 |
| -] |
19 |
| - |
20 | 4 | export const formatDateRange = (startDate: string, endDate: string) => {
|
21 | 5 | // Parse the input dates
|
22 | 6 | // .replace(/-/g, "/") ==> Fixes Safari Invalid date
|
| 7 | + |
23 | 8 | const start = new Date(startDate.replace(/-/g, "/"))
|
24 | 9 | const end = new Date(endDate.replace(/-/g, "/"))
|
25 | 10 |
|
26 |
| - // Extract day and month from the start and end dates |
27 |
| - const startDay = start.getDate() |
28 |
| - const startMonth = months[start.getMonth()] |
29 |
| - const endDay = end.getDate() |
30 |
| - const endMonth = months[end.getMonth()] |
31 |
| - |
32 |
| - // Extract year from start and end dates |
33 |
| - const startYear = start.getFullYear() |
34 |
| - const endYear = end.getFullYear() |
35 |
| - |
36 |
| - // Format the year (if start and end years are the same, only show the year once) |
37 |
| - const startYearFormatted = startYear.toString() |
38 |
| - const endYearFormatted = endYear.toString() |
39 |
| - |
40 |
| - // Format the date range string |
| 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 |
41 | 29 | let dateRangeString
|
42 |
| - |
43 |
| - if (startYear === endYear && start.getMonth() === end.getMonth()) { |
44 |
| - dateRangeString = `${startDay}-${endDay} ${startMonth} ${startYearFormatted}` |
45 |
| - } else if (startYear === endYear) { |
46 |
| - dateRangeString = `${startDay} ${startMonth} - ${endDay} ${endMonth} ${startYearFormatted}` |
| 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}` |
47 | 38 | } else {
|
48 |
| - dateRangeString = `${startDay} ${startMonth} ${startYearFormatted} - ${endDay} ${endMonth} ${endYearFormatted}` |
49 |
| - } |
50 |
| - if (startDate === endDate) { |
51 |
| - dateRangeString = `${startDay} ${startMonth} ${startYearFormatted}` |
| 39 | + // If different months or years, show as "12 Jul - 14 Aug" |
| 40 | + dateRangeString = `${startDayMonth}-${endDayMonth}` |
52 | 41 | }
|
53 | 42 |
|
54 | 43 | return dateRangeString
|
|
0 commit comments