Skip to content

Commit 3fe210a

Browse files
Merge pull request #286 from Asraye/feat/12hr-time
feat: 12hr time
2 parents 691a15f + 0be9fe4 commit 3fe210a

4 files changed

Lines changed: 56 additions & 8 deletions

File tree

‎src/common/date.ts‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
import { useLocalStorage, StorageKeys } from "@/common/localStorage";
12
import dayjs from "dayjs";
23
import duration from "dayjs/plugin/duration";
34
dayjs.extend(duration);
45

6+
export const [timeFormat, setTimeFormat] = useLocalStorage<"12hr" | "24hr">(
7+
StorageKeys.TIME_FORMAT,
8+
"24hr",
9+
true
10+
);
11+
512
// Weeks are not working as intended. update package when this gets merged.
613
// https://github.com/iamkun/dayjs/pull/2811
714

815
// make a function where if the number is less than 10, it will add a 0 in front of it
16+
917
function pad(num: number) {
1018
return num < 10 ? `0${num}` : num;
1119
}
@@ -14,28 +22,42 @@ function pad(num: number) {
1422
export function formatTimestamp(timestamp: number, seconds = false) {
1523
const date = new Date(timestamp);
1624
const today = new Date();
17-
1825
const yesterday = new Date();
1926
yesterday.setDate(yesterday.getDate() - 1);
2027

2128
const sameYear = today.getFullYear() === date.getFullYear();
29+
const format = timeFormat();
30+
31+
const hours = date.getHours();
32+
const minutes = pad(date.getMinutes());
33+
const secondsText = seconds ? `:${pad(date.getSeconds())}` : "";
34+
35+
let formattedHours: string | number = hours;
36+
let ampm = "";
37+
38+
if (format === "12hr") {
39+
ampm = hours >= 12 ? " PM" : " AM";
40+
formattedHours = hours % 12 || 12;
41+
}
42+
43+
if (format === "24hr") {
44+
formattedHours = pad(hours);
45+
}
2246

2347
if (
2448
sameYear &&
2549
date.getDate() === today.getDate() &&
2650
date.getMonth() === today.getMonth()
2751
) {
28-
return `${pad(date.getHours())}:${pad(date.getMinutes())}${
29-
seconds ? `:${pad(date.getSeconds())}` : ""
30-
}`;
52+
return `${formattedHours}:${minutes}${secondsText}${ampm}`;
3153
} else if (sameYear && yesterday.toDateString() === date.toDateString()) {
32-
return `Yesterday at ${pad(date.getHours())}:${pad(date.getMinutes())}`;
54+
return `Yesterday at ${formattedHours}:${minutes}${ampm}`;
3355
} else {
3456
return `${Intl.DateTimeFormat("en-GB", {
3557
day: "2-digit",
3658
month: "short",
3759
year: "numeric",
38-
}).format(date)} at ${pad(date.getHours())}:${pad(date.getMinutes())}`;
60+
}).format(date)} at ${formattedHours}:${minutes}${ampm}`;
3961
}
4062
}
4163

@@ -59,7 +81,6 @@ export const fullDateTime = (timestamp: number) => {
5981
)}`;
6082
};
6183

62-
// get days ago from timestamp
6384
export function getDaysAgo(timestamp: number) {
6485
const rtf = new Intl.RelativeTimeFormat("en", {
6586
numeric: "auto",
@@ -287,3 +308,4 @@ function pluralize(
287308
count + " " + (count > 1 ? `${word}s` : word + (suffix ? ` ${suffix}` : ""))
288309
);
289310
}
311+

‎src/common/localStorage.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const StorageKeys = {
3333
HIDDEN_ANNOUNCEMENT_IDS: "hiddenAnnouncementIds",
3434
CHAT_BAR_OPTIONS: "chatBarOptions",
3535
MENTION_REPLIES: "mentionReplies",
36+
TIME_FORMAT: "timeFormat",
3637
} as const;
3738

3839
export type StorageKeys = (typeof StorageKeys)[keyof typeof StorageKeys];

‎src/components/settings/InterfaceSettings.tsx‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import env from "@/common/env";
2525
import style from "./InterfaceSettings.module.css";
2626
import { useNavigate } from "solid-navigator";
2727
import { FlexColumn } from "../ui/Flexbox";
28+
import { timeFormat, setTimeFormat } from "@/common/date";
2829

2930
export default function InterfaceSettings() {
3031
const { header } = useStore();
@@ -44,6 +45,7 @@ export default function InterfaceSettings() {
4445
<BreadcrumbItem title={t("settings.drawer.interface")} />
4546
</Breadcrumb>
4647
<ThemesBlock />
48+
<TimeFormatSetting />
4749
<BlurEffect />
4850
<AdvancedMarkup />
4951
<ChatBar />
@@ -172,6 +174,26 @@ export function ThemesBlock() {
172174
);
173175
}
174176

177+
function TimeFormatSetting() {
178+
const toggleTimeFormat = () => {
179+
setTimeFormat(timeFormat() === "12hr" ? "24hr" : "12hr");
180+
};
181+
182+
return (
183+
<SettingsBlock
184+
icon="schedule"
185+
label={t("settings.interface.timeTitle")}
186+
description={t(
187+
timeFormat() === "24hr"
188+
? t("settings.interface.24HourFormatDescription")
189+
: t("settings.interface.12HourFormatDescription")
190+
)}
191+
>
192+
<Checkbox checked={timeFormat() === "12hr"} onChange={toggleTimeFormat} />
193+
</SettingsBlock>
194+
);
195+
}
196+
175197
function ChatBar() {
176198
const { isMobileAgent } = useWindowProperties();
177199
const [chatBarOptions, setChatBarOptions] = useChatBarOptions();

‎src/locales/list/en-gb.json‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,10 @@
571571
"disableAdvancedMarkupBarDescription": "Prevent the advanced markup bar from appearing.",
572572
"customizeColors": "Customize Colors",
573573
"customizeColorsDescription": "Edit colors for the current theme.",
574-
"chatBarOptions": "Chat bar Options"
574+
"chatBarOptions": "Chat bar Options",
575+
"timeTitle": "Toggle Time Format",
576+
"12HourFormatDescription": "Display times in 12-hour format with AM/PM.",
577+
"24HourFormatDescription": "Display times in 24-hour format."
575578
},
576579
"window": {
577580
"title": "Settings - Window Settings",

0 commit comments

Comments
 (0)