| 1 |
export function useDateField() { |
| 2 |
function parseDateString( |
| 3 |
dateStr: string, |
| 4 |
format?: string, |
| 5 |
): { day: string; month: string; year: string } { |
| 6 |
if (!dateStr) { |
| 7 |
return { day: '', month: '', year: '' } |
| 8 |
} |
| 9 |
|
| 10 |
// Try to parse based on separators |
| 11 |
const separators = ['-', '/', '.'] |
| 12 |
let parts: string[] = [] |
| 13 |
|
| 14 |
for (const sep of separators) { |
| 15 |
if (dateStr.includes(sep)) { |
| 16 |
parts = dateStr.split(sep) |
| 17 |
break |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
if (parts.length !== 3) { |
| 22 |
return { day: '', month: '', year: '' } |
| 23 |
} |
| 24 |
|
| 25 |
// Determine format (default to MM/DD/YYYY if not provided) |
| 26 |
const dateFormat = (format || 'MM/DD/YYYY').toUpperCase() |
| 27 |
|
| 28 |
let day = '' |
| 29 |
let month = '' |
| 30 |
let year = '' |
| 31 |
|
| 32 |
const formatParts = dateFormat.split(/[/.-]/) |
| 33 |
formatParts.forEach((part, index) => { |
| 34 |
const upperPart = part.toUpperCase() |
| 35 |
if (upperPart === 'DD' || upperPart === 'D') day = parts[index] |
| 36 |
if (upperPart === 'MM' || upperPart === 'M') month = parts[index] |
| 37 |
if (upperPart === 'YYYY' || upperPart === 'YY') year = parts[index] |
| 38 |
}) |
| 39 |
|
| 40 |
return { day, month, year } |
| 41 |
} |
| 42 |
|
| 43 |
function formatDateString( |
| 44 |
day: string, |
| 45 |
month: string, |
| 46 |
year: string, |
| 47 |
format: string = 'MM/DD/YYYY', |
| 48 |
): string { |
| 49 |
if (!day || !month || !year) return '' |
| 50 |
|
| 51 |
const separator = format.includes('/') ? '/' : format.includes('-') ? '-' : '.' |
| 52 |
|
| 53 |
const formatParts = format.split(/[/.-]/) |
| 54 |
const result = formatParts.map((part) => { |
| 55 |
const upperPart = part.toUpperCase() |
| 56 |
if (upperPart === 'DD' || upperPart === 'D') { |
| 57 |
return upperPart === 'DD' ? day.padStart(2, '0') : day |
| 58 |
} |
| 59 |
if (upperPart === 'MM' || upperPart === 'M') { |
| 60 |
return upperPart === 'MM' ? month.padStart(2, '0') : month |
| 61 |
} |
| 62 |
if (upperPart === 'YYYY') return year |
| 63 |
if (upperPart === 'YY') return year.slice(-2) |
| 64 |
return part |
| 65 |
}) |
| 66 |
|
| 67 |
return result.join(separator) |
| 68 |
} |
| 69 |
|
| 70 |
function isValidDate( |
| 71 |
day: string | number, |
| 72 |
month: string | number, |
| 73 |
year: string | number, |
| 74 |
): boolean { |
| 75 |
const d = typeof day === 'string' ? parseInt(day, 10) : day |
| 76 |
const m = typeof month === 'string' ? parseInt(month, 10) : month |
| 77 |
const y = typeof year === 'string' ? parseInt(year, 10) : year |
| 78 |
|
| 79 |
if (isNaN(d) || isNaN(m) || isNaN(y)) return false |
| 80 |
if (m < 1 || m > 12) return false |
| 81 |
if (d < 1 || d > 31) return false |
| 82 |
if (y < 1000 || y > 9999) return false |
| 83 |
|
| 84 |
// Check day validity for month |
| 85 |
const daysInMonth = new Date(y, m, 0).getDate() |
| 86 |
return d <= daysInMonth |
| 87 |
} |
| 88 |
|
| 89 |
function getDaysArray(): Array<{ label: string; value: string }> { |
| 90 |
return Array.from({ length: 31 }, (_, i) => { |
| 91 |
const day = (i + 1).toString().padStart(2, '0') |
| 92 |
return { label: day, value: day } |
| 93 |
}) |
| 94 |
} |
| 95 |
|
| 96 |
function getMonthsArray(): Array<{ label: string; value: string }> { |
| 97 |
return Array.from({ length: 12 }, (_, i) => { |
| 98 |
const month = (i + 1).toString().padStart(2, '0') |
| 99 |
return { label: month, value: month } |
| 100 |
}) |
| 101 |
} |
| 102 |
|
| 103 |
function getYearsArray( |
| 104 |
minYear?: number, |
| 105 |
maxYear?: number, |
| 106 |
): Array<{ label: string; value: string }> { |
| 107 |
const currentYear = new Date().getFullYear() |
| 108 |
const min = minYear || currentYear - 100 |
| 109 |
const max = maxYear || currentYear + 100 |
| 110 |
|
| 111 |
const years: Array<{ label: string; value: string }> = [] |
| 112 |
for (let year = min; year <= max; year++) { |
| 113 |
years.push({ label: year.toString(), value: year.toString() }) |
| 114 |
} |
| 115 |
return years |
| 116 |
} |
| 117 |
|
| 118 |
function toISOFormat(dateStr: string, format?: string): string { |
| 119 |
const parsed = parseDateString(dateStr, format) |
| 120 |
if (!parsed.day || !parsed.month || !parsed.year) return '' |
| 121 |
return `${parsed.year}-${parsed.month.padStart(2, '0')}-${parsed.day.padStart(2, '0')}` |
| 122 |
} |
| 123 |
|
| 124 |
function fromISOFormat(isoDate: string, format: string = 'MM/DD/YYYY'): string { |
| 125 |
if (!isoDate) return '' |
| 126 |
const [year, month, day] = isoDate.split('-') |
| 127 |
return formatDateString(day, month, year, format) |
| 128 |
} |
| 129 |
|
| 130 |
return { |
| 131 |
parseDateString, |
| 132 |
formatDateString, |
| 133 |
isValidDate, |
| 134 |
getDaysArray, |
| 135 |
getMonthsArray, |
| 136 |
getYearsArray, |
| 137 |
toISOFormat, |
| 138 |
fromISOFormat, |
| 139 |
} |
| 140 |
} |
| 141 |
|