forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

format.ts51 lines, 2.0 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/** Date only, no time — for stacked date/time cells. */
12export function formatDate(ms: number | null | undefined, locale: Locale = "en"): string {
13 if (ms == null) return "—";
14 return new Intl.DateTimeFormat(locale === "tr" ? "tr-TR" : "en-US", {
15 dateStyle: "medium",
16 }).format(new Date(ms));
17}
18
19/** Convert datetime-local input value to unix ms, or null if empty. */
20export function localInputToMs(value: string): number | null {
21 if (!value) return null;
22 const t = new Date(value).getTime();
23 return Number.isNaN(t) ? null : t;
24}
25
26/** Convert unix ms to datetime-local input value. */
27export function msToLocalInput(ms: number | null | undefined): string {
28 if (ms == null) return "";
29 const d = new Date(ms);
30 const pad = (n: number) => String(n).padStart(2, "0");
31 return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
32}
33
34export function formatDurationMinutes(ms: number | null | undefined, locale: Locale = "en"): string {
35 if (ms == null) return "—";
36 return locale === "tr" ? `${Math.round(ms / 60_000)} dk` : `${Math.round(ms / 60_000)} min`;
37}
38
39export function formatDurationClock(ms: number | null | undefined): string {
40 if (ms == null) return "--:--";
41 const totalSeconds = Math.max(0, Math.floor(ms / 1000));
42 const minutes = Math.floor(totalSeconds / 60);
43 const seconds = totalSeconds % 60;
44 return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
45}
46
47export function examDurationMs(durationMs: number | null | undefined, startsAt: number | null, endsAt: number | null): number | null {
48 if (durationMs != null) return durationMs;
49 if (startsAt == null || endsAt == null || endsAt <= startsAt) return null;
50 return endsAt - startsAt;
51}