const monthsInId = [ 'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember', ] export function getAge(dateString: string, comparedDate?: string): { idFormat: string; extFormat: string } { const birthDate = new Date(dateString) const today = new Date() if (comparedDate) { const comparedDateObj = new Date(comparedDate) today.setFullYear(comparedDateObj.getFullYear()) today.setMonth(comparedDateObj.getMonth()) today.setDate(comparedDateObj.getDate()) } // Format the date part const options: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'long', year: 'numeric' } const idFormat = birthDate.toLocaleDateString('id-ID', options) // Calculate age let years = today.getFullYear() - birthDate.getFullYear() let months = today.getMonth() - birthDate.getMonth() let days = today.getDate() - birthDate.getDate() if (months < 0 || (months === 0 && days < 0)) { years-- months += 12 } if (days < 0) { const prevMonth = new Date(today.getFullYear(), today.getMonth() - 1, 0) days += prevMonth.getDate() months-- } // Format the age part let extFormat = '' if ([years, months, days].filter(Boolean).join(' ')) { extFormat = `${years} Tahun ${months} Bulan ${days} Hari` } else { extFormat = '0' } return { idFormat, extFormat, } } // Date selection: default to today - today export function getFormatDateId(date: Date) { const dd = String(date.getDate()).padStart(2, '0') const mm = monthsInId[date.getMonth()] const yyyy = date.getFullYear() return `${dd} ${mm} ${yyyy}` } export function formatDateYyyyMmDd(isoDateString: string): string { const date = new Date(isoDateString); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } // Function to check if date is invalid (like "0001-01-01T00:00:00Z") export function isValidDate(dateString: string | null | undefined): boolean { if (!dateString) return false // Check for invalid date patterns if (dateString.startsWith('0001-01-01')) return false try { const date = new Date(dateString) return !isNaN(date.getTime()) } catch { return false } }