const dte = /\:\d{2}\.\d{3}Z$/; const now = new Date(); const utcOffset = -now.getTimezoneOffset() / 60; // Negate the offset to match local time adjustments const toShortString = (date: Date): string => { return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}T${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`; }; const moveDate = (str: string): string => { let date = new Date(str); // Use the full ISO date let newHour = date.getUTCHours() + utcOffset; // Adjust hours with the correct offset date.setUTCHours(newHour); // Set the new hours based on UTC offset return toShortString(date); // Return the formatted string }; export const toDate = (str: string | number): Date => { str = str.toString(); const date = new Date() date.setFullYear(parseInt(str.slice(0, 4))); date.setMonth(parseInt(str.slice(5, 7)) - 1); date.setDate(parseInt(str.slice(8, 10))); date.setHours(parseInt(str.slice(11, 13))); date.setMinutes(parseInt(str.slice(14, 16))); date.setSeconds(0); return date; } const shortenString = (date: string): string => { return new Date(date).toISOString().replace(dte, ''); }; /* Returns date as string for use as the value for HTML Date Input */ export const toInputDate = (date: string | number): string => { const d = new Date(date); return `${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')}`; }; /* Returns date as string for use as the value for HTML Date-Time Input */ export const toDateTimeInput = (date: string | number): string => { return moveDate(shortenString(new Date(date).toISOString())); };