time
shuip/timeFreeUtility
shuip publishes no description for this item.
Install it
npx shadcn@latest add https://shuip.plvo.dev/r/time.jsonSource
1export const MINUTE_STEP = 5;23export const HOUR_OPTIONS: string[] = Array.from({ length: 24 }, (_, i) => String(i).padStart(2, '0'));45export const MINUTE_OPTIONS: string[] = Array.from({ length: 60 / MINUTE_STEP }, (_, i) =>6 String(i * MINUTE_STEP).padStart(2, '0'),7);89export type TimeRangeValue = { start: string; end: string };1011export function splitTime(time: string): { hour: string; minute: string } {12 const [hour = '', minute = ''] = time.split(':');13 return { hour, minute };14}1516export function joinTime(hour: string, minute: string): string {17 if (!hour && !minute) return '';18 return `${hour || '00'}:${minute || '00'}`;19}2021export function toMinutes(time: string): number | null {22 const { hour, minute } = splitTime(time);23 if (!hour || !minute) return null;24 const h = Number(hour);25 const m = Number(minute);26 if (Number.isNaN(h) || Number.isNaN(m)) return null;27 return h * 60 + m;28}2930export function fromMinutes(total: number): string {31 const clamped = Math.max(0, Math.min(total, 24 * 60 - MINUTE_STEP));32 const h = Math.floor(clamped / 60);33 const m = clamped % 60;34 return `${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}`;35}3637export function addOneHour(time: string): string {38 const minutes = toMinutes(time);39 if (minutes === null) return time;40 return fromMinutes(minutes + 60);41}4243export function nextSlot(time: string): string {44 const minutes = toMinutes(time);45 if (minutes === null) return time;46 return fromMinutes(minutes + MINUTE_STEP);47}4849export function isHourDisabled(hour: string, bounds?: { min?: string; max?: string }): boolean {50 if (!hour) return false;51 const h = Number(hour);52 if (Number.isNaN(h)) return false;53 const hourStart = h * 60;54 const hourEnd = h * 60 + 55;55 const min = bounds?.min ? toMinutes(bounds.min) : null;56 const max = bounds?.max ? toMinutes(bounds.max) : null;57 if (min !== null && hourEnd < min) return true;58 if (max !== null && hourStart > max) return true;59 return false;60}6162export function isMinuteDisabled(minute: string, hour: string, bounds?: { min?: string; max?: string }): boolean {63 if (!hour || !minute) return false;64 const t = toMinutes(joinTime(hour, minute));65 if (t === null) return false;66 const min = bounds?.min ? toMinutes(bounds.min) : null;67 const max = bounds?.max ? toMinutes(bounds.max) : null;68 if (min !== null && t < min) return true;69 if (max !== null && t > max) return true;70 return false;71}
