/* provide utilities for converting between Date objects and formatted date/time strings, allowing for flexible handling of date and time data within React applications. */ interface DateTimeFormat { date: string; time: string; } export const useDateTimeConverter = () => { // Function to convert a Date object to a date and time string const dateToString = (date: Date | null, format: DateTimeFormat): string => { if (!date) return ''; const dateString = formatDate(date, format.date); const timeString = formatTime(date, format.time); return `${dateString} ${timeString}`; }; // Function to convert a date and time string to a Date object const stringToDate = (dateTimeString: string, format: DateTimeFormat): Date | null => { if (!dateTimeString) return null; const [dateString, timeString] = dateTimeString.split(' '); const date = parseDate(dateString, format.date); const time = parseTime(timeString, format.time); if (date && time) { return new Date(date.getFullYear(), date.getMonth(), date.getDate(), time.getHours(), time.getMinutes(), time.getSeconds()); } return null; }; // Function to format the date portion of a Date object const formatDate = (date: Date, format: string): string => { if (!date) return ''; const year = date.getFullYear(); const month = (date.getMonth() + 1).toString().padStart(2, '0'); const day = date.getDate().toString().padStart(2, '0'); return format.replace('YYYY', year.toString()).replace('MM', month).replace('DD', day); }; // Function to format the time portion of a Date object const formatTime = (date: Date, format: string): string => { if (!date) return ''; const hours = date.getHours().toString().padStart(2, '0'); const minutes = date.getMinutes().toString().padStart(2, '0'); const seconds = date.getSeconds().toString().padStart(2, '0'); return format.replace('HH', hours).replace('mm', minutes).replace('ss', seconds); }; // Function to parse the time string and construct a Date object const parseTime = (timeString: string, format: string): Date | null => { if (!timeString) return null; const hoursIndex = format.indexOf('HH'); const minutesIndex = format.indexOf('mm'); const secondsIndex = format.indexOf('ss'); // Ensure all required components are present in the format if (hoursIndex === -1 || minutesIndex === -1 || secondsIndex === -1) { return null; } const hours = parseInt(timeString.substr(hoursIndex, 2), 10); const minutes = parseInt(timeString.substr(minutesIndex, 2), 10); const seconds = parseInt(timeString.substr(secondsIndex, 2), 10); // Ensure parsed components are valid if (isNaN(hours) || isNaN(minutes) || isNaN(seconds)) { return null; } // Construct a Date object with January 1, 1900 return new Date(0, 0, 0, hours, minutes, seconds); }; // Function to parse the date string and construct a Date object const parseDate = (dateString: string, format: string): Date | null => { if (!dateString) return null; const yearIndex = format.indexOf('YYYY'); const monthIndex = format.indexOf('MM'); const dayIndex = format.indexOf('DD'); // Ensure all required components are present in the format if (yearIndex === -1 || monthIndex === -1 || dayIndex === -1) { return null; } const year = parseInt(dateString.substr(yearIndex, 4), 10); const month = parseInt(dateString.substr(monthIndex, 2), 10) - 1; const day = parseInt(dateString.substr(dayIndex, 2), 10); // Ensure parsed components are valid if (isNaN(year) || isNaN(month) || isNaN(day)) { return null; } // Construct a Date object return new Date(year, month, day); }; // Return the functions to use in a component return { stringToDate, dateToString, formatTime, formatDate, parseTime, parseDate, }; }