Files
co-lite/solvway-admin-portal/app/lib/date-input.ts
T
ngqileandClaude Opus 5.5 c908fbe9fc feat(admin-portal): port babyboo's date picker and calendar
Adds react-day-picker 10.0.1 (it brings date-fns). The portal bans native date
inputs, and the events and leads forms need a date field.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-27 00:23:56 +08:00

25 lines
1.0 KiB
TypeScript

const pad = (n: number) => String(n).padStart(2, "0");
/** Local-midnight Date for y/m/d, or undefined when the parts are not a real calendar day. */
function realDate(y: number, m: number, d: number): Date | undefined {
const dt = new Date(y, m - 1, d);
return dt.getFullYear() === y && dt.getMonth() === m - 1 && dt.getDate() === d ? dt : undefined;
}
/** "yyyy-MM-dd" → "DD/MM/YYYY"; "" for anything else. */
export function formatDmy(iso: string): string {
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(iso);
return m ? `${m[3]}/${m[2]}/${m[1]}` : "";
}
/** "yyyy-MM-dd" → local-midnight Date; undefined for "" or an impossible date. */
export function isoToDate(iso: string): Date | undefined {
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(iso);
return m ? realDate(Number(m[1]), Number(m[2]), Number(m[3])) : undefined;
}
/** Date → "yyyy-MM-dd" from LOCAL parts (toISOString would shift the day in UTC+8). */
export function dateToIso(d: Date): string {
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
}