forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBd962efeBurak Boduroğlu4 months ago146 commits

format.ts35 lines, 1.4 KiBHistoryRawDownload
1import type { Locale } from "@/i18n/messages";
2
3export function formatDateTime(ms: number | null | undefined, locale: Locale = "en"): string {
4 if (ms == null) return "—";
5 return new Intl.DateTimeFormat(locale === "tr" ? "tr-TR" : "en-US", {
6 dateStyle: "medium",
7 timeStyle: "short",
8 }).format(new Date(ms));
9}
10
11/** Convert datetime-local input value to unix ms, or null if empty. */
12export function localInputToMs(value: string): number | null {
13 if (!value) return null;
14 const t = new Date(value).getTime();
15 return Number.isNaN(t) ? null : t;
16}
17
18/** Convert unix ms to datetime-local input value. */
19export function msToLocalInput(ms: number | null | undefined): string {
20 if (ms == null) return "";
21 const d = new Date(ms);
22 const pad = (n: number) => String(n).padStart(2, "0");
23 return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
24}
25
26export function formatDurationMinutes(ms: number | null | undefined, locale: Locale = "en"): string {
27 if (ms == null) return "—";
28 return locale === "tr" ? `${Math.round(ms / 60_000)} dk` : `${Math.round(ms / 60_000)} min`;
29}
30
31export function examDurationMs(durationMs: number | null | undefined, startsAt: number | null, endsAt: number | null): number | null {
32 if (durationMs != null) return durationMs;
33 if (startsAt == null || endsAt == null || endsAt <= startsAt) return null;
34 return endsAt - startsAt;
35}