forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB0069847Burak Boduroğlu2 months ago552 commits

datetime-input.ts56 lines, 2.4 KiBHistoryRawDownload
1/** dd/mm/yyyy + HH:mm form-input <-> unix-ms helpers, matching event-form's local pattern. */
2
3export function dateInputFromMs(ms: number | null | undefined): string {
4 if (ms == null) return "";
5 const d = new Date(ms);
6 const pad = (n: number) => String(n).padStart(2, "0");
7 return `${pad(d.getDate())}/${pad(d.getMonth() + 1)}/${d.getFullYear()}`;
8}
9
10export function timeInputFromMs(ms: number | null | undefined): string {
11 if (ms == null) return "";
12 const d = new Date(ms);
13 const pad = (n: number) => String(n).padStart(2, "0");
14 return `${pad(d.getHours())}:${pad(d.getMinutes())}`;
15}
16
17export function dateTimeInputToMs(date: string, time: string): number | null {
18 const dateMatch = date.trim().match(/^(\d{2})\/(\d{2})\/(\d{4})$/);
19 const timeMatch = time.trim().match(/^(\d{2}):(\d{2})$/);
20 if (!dateMatch || !timeMatch) return null;
21 const [, dayRaw, monthRaw, yearRaw] = dateMatch;
22 const [, hourRaw, minuteRaw] = timeMatch;
23 const day = Number(dayRaw);
24 const month = Number(monthRaw);
25 const year = Number(yearRaw);
26 const hour = Number(hourRaw);
27 const minute = Number(minuteRaw);
28 const d = new Date(year, month - 1, day, hour, minute, 0, 0);
29 if (d.getFullYear() !== year || d.getMonth() !== month - 1 || d.getDate() !== day || d.getHours() !== hour || d.getMinutes() !== minute) return null;
30 return d.getTime();
31}
32
33/** dd/mm/yyyy at end-of-day, for a "repeat until" date with no time input. */
34export function dateInputToEndOfDayMs(date: string): number | null {
35 const dateMatch = date.trim().match(/^(\d{2})\/(\d{2})\/(\d{4})$/);
36 if (!dateMatch) return null;
37 const [, dayRaw, monthRaw, yearRaw] = dateMatch;
38 const day = Number(dayRaw);
39 const month = Number(monthRaw);
40 const year = Number(yearRaw);
41 const d = new Date(year, month - 1, day, 23, 59, 0, 0);
42 if (d.getFullYear() !== year || d.getMonth() !== month - 1 || d.getDate() !== day) return null;
43 return d.getTime();
44}
45
46/** yyyy-mm-dd (API calendar date) → dd/mm/yyyy (DatePicker value). */
47export function isoDateToInput(iso: string): string {
48 const match = iso.trim().match(/^(\d{4})-(\d{2})-(\d{2})$/);
49 return match ? `${match[3]}/${match[2]}/${match[1]}` : "";
50}
51
52/** dd/mm/yyyy (DatePicker value) → yyyy-mm-dd, or "" when incomplete. */
53export function inputDateToIso(date: string): string {
54 const match = date.trim().match(/^(\d{2})\/(\d{2})\/(\d{4})$/);
55 return match ? `${match[3]}-${match[2]}-${match[1]}` : "";
56}