Skip to content

Commit 26fdd91

Browse files
Frontend/i18n: Move monthFormatter to localizeDateTime infra. (#8123)
1 parent 5279ebd commit 26fdd91

3 files changed

Lines changed: 40 additions & 15 deletions

File tree

app/web/features/profile/view/ReferenceListItem.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { COMMUNITIES, GLOBAL } from "i18n/namespaces";
77
import { useTranslation } from "next-i18next";
88
import { LiteUser } from "proto/api_pb";
99
import { Reference } from "proto/references_pb";
10-
import { monthFormatter, timestamp2Date } from "utils/date";
10+
import { localizeYearMonth, timestamp2Date } from "utils/date";
1111

1212
export const REFERENCE_LIST_ITEM_TEST_ID = "reference-list-item";
1313

@@ -63,9 +63,10 @@ export default function ReferenceListItem({
6363
)}
6464
{reference.writtenTime && (
6565
<Pill variant="rounded">
66-
{monthFormatter(locale).format(
67-
timestamp2Date(reference.writtenTime),
68-
)}
66+
{localizeYearMonth(timestamp2Date(reference.writtenTime), {
67+
locale,
68+
abbreviate: true,
69+
})}
6970
</Pill>
7071
)}
7172
</StyledBadgesContainer>

app/web/features/profile/view/userLabels.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import {
1010
User,
1111
} from "proto/api_pb";
1212
import { theme } from "theme";
13-
import { localizeDateTime, monthFormatter, timestamp2Date } from "utils/date";
13+
import {
14+
localizeDateTime,
15+
localizeYearMonth,
16+
timestamp2Date,
17+
UTC_TIMEZONE,
18+
} from "utils/date";
1419
import dayjs from "utils/dayjs";
1520
import { timeAgo, TimeUnit } from "utils/timeAgo";
1621

@@ -264,14 +269,17 @@ export const RemainingAboutLabels = ({ user }: Props) => {
264269
label={t("profile:heading.joined")}
265270
text={
266271
user.joined
267-
? monthFormatter(locale).format(timestamp2Date(user.joined))
272+
? localizeYearMonth(timestamp2Date(user.joined), {
273+
locale,
274+
abbreviate: true,
275+
})
268276
: ""
269277
}
270278
/>
271279
<LabelAndText
272280
label={t("profile:heading.local_time")}
273281
text={localizeDateTime(dayjs(), {
274-
timezone: user.timezone || "Etc/UTC",
282+
timezone: user.timezone || UTC_TIMEZONE,
275283
locale,
276284
includeDate: false,
277285
})}

app/web/utils/date.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb";
44
import daysjs, { Dayjs } from "./dayjs";
55
import { dayMillis } from "./timeAgo";
66

7-
const monthFormatter = (locale: string) =>
8-
new Intl.DateTimeFormat(locale, {
9-
month: "short",
10-
year: "numeric",
11-
});
12-
137
const numNights = (date1: string, date2: string) => {
148
const diffTime = Date.parse(date1) - Date.parse(date2);
159
const diffDays = Math.ceil(diffTime / dayMillis);
@@ -27,6 +21,8 @@ interface LocalizeDateTimeParams {
2721
timezone?: string | typeof BROWSER_TIMEZONE;
2822
/// Whether to include the date (defaults to true).
2923
includeDate?: boolean;
24+
/// If including the date, whether to include the day (defaults to true).
25+
includeDay?: boolean;
3026
/// If including the date, whether to include the day of week (defaults to false).
3127
includeDayOfWeek?: boolean;
3228
/// Whether to include the time (defaults to true).
@@ -49,6 +45,24 @@ export function localizeDateTime(
4945
return format.format(date);
5046
}
5147

48+
/// Localizes only the year and month of a date.
49+
export function localizeYearMonth(
50+
date: Date | Dayjs,
51+
args: {
52+
timezone?: string | typeof BROWSER_TIMEZONE;
53+
locale: string;
54+
abbreviate?: boolean;
55+
},
56+
): string {
57+
return localizeDateTime(date, {
58+
timezone: args.timezone,
59+
locale: args.locale,
60+
abbreviate: args.abbreviate,
61+
includeDay: false,
62+
includeTime: false,
63+
});
64+
}
65+
5266
/// Localizes a range of date and times as a string.
5367
export function localizeDateTimeRange(
5468
start: Date | Dayjs,
@@ -93,7 +107,9 @@ function createIntlDateTimeFormat(
93107
if (args.includeDate !== false) {
94108
options.year = "numeric";
95109
options.month = args.abbreviate ? "short" : "long";
96-
options.day = "numeric";
110+
if (args.includeDay !== false) {
111+
options.day = "numeric";
112+
}
97113
if (args.includeDayOfWeek) {
98114
options.weekday = args.abbreviate ? "short" : "long";
99115
}
@@ -128,4 +144,4 @@ function isSameOrFutureDate(date1: Dayjs, date2: Dayjs): boolean {
128144
return isSameDate(date1, date2) || date1.isAfter(date2);
129145
}
130146

131-
export { isSameOrFutureDate, monthFormatter, numNights, timestamp2Date };
147+
export { isSameOrFutureDate, numNights, timestamp2Date };

0 commit comments

Comments
 (0)