Skip to content

Commit d73d634

Browse files
committed
fix: change DateTimeFormat to Intl
1 parent ad86dff commit d73d634

File tree

2 files changed

+35
-45
lines changed

2 files changed

+35
-45
lines changed

src/components/UpcomingEventsList.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import EventCard from "@/components/EventCard"
1111
import InfoBanner from "@/components/InfoBanner"
1212
import InlineLink from "@/components/Link"
1313

14-
import { months } from "@/lib/utils/date"
1514
import { trackCustomEvent } from "@/lib/utils/matomo"
1615
import { getLocaleTimestamp } from "@/lib/utils/time"
1716

@@ -72,10 +71,12 @@ const UpcomingEventsList = () => {
7271
})
7372
const groupedEvents = _.groupBy(formattedEvents, ({ startDate }) => {
7473
// .replace(/-/g, "/") ==> Fixes Safari Invalid date
75-
const month = new Date(startDate.replace(/-/g, "/")).getMonth()
76-
const year = new Date(startDate.replace(/-/g, "/")).getFullYear()
77-
const monthName = months[month]
78-
return `${monthName} ${year}`
74+
const start = new Date(startDate.replace(/-/g, "/"))
75+
const formatYearMonth = new Intl.DateTimeFormat(undefined, {
76+
month: "short",
77+
year: "numeric",
78+
}).format(start)
79+
return `${formatYearMonth}`
7980
})
8081

8182
setMonthGroupedEvents(groupedEvents)

src/lib/utils/date.ts

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,43 @@
11
export const dateToString = (published: Date | string) =>
22
new Date(published).toISOString().split("T")[0]
33

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-
204
export const formatDateRange = (startDate: string, endDate: string) => {
215
// Parse the input dates
226
// .replace(/-/g, "/") ==> Fixes Safari Invalid date
7+
238
const start = new Date(startDate.replace(/-/g, "/"))
249
const end = new Date(endDate.replace(/-/g, "/"))
2510

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
4129
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}`
4738
} 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}`
5241
}
5342

5443
return dateRangeString

0 commit comments

Comments
 (0)