export function formatInputDate (date: Date, locale: string): string { // only it-IT try { const year = date.getFullYear().toString() let month = (date.getMonth() + 1).toString() let day = date.getDate().toString() if (day.length === 1) { day = `0${day}` } if (month.length === 1) { month = `0${month}` } return `${day}/${month}/${year}` } catch { return '' } } export function parseInputDate (value: string, locale: string): Date | null { // only it-IT const dateRegex = /^(0?[1-9]|[12][0-9]|3[01])[/](0?[1-9]|1[012])[/]\d{4}$/ if (!value.match(dateRegex)) { return null } const parts = value.split('/') const day = parseInt(parts[0]) const month = parseInt(parts[1]) const year = parseInt(parts[2]) const parsedDate = new Date(Date.UTC(year, month - 1, day)) const formattedDate = formatInputDate(parsedDate, locale) if (formattedDate === value) { return parsedDate } else { return null } } export function parseDate (isoString: string): Date { return new Date(isoString) } export function formatDate (date: Date, locale: string): string { const months = ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'] const year = date.getFullYear() const month = months[date.getMonth()] const day = date.getDate() return `${day} ${month} ${year}` } export function formatTime (date: Date, locale: string): string { const hour = date.getHours() const minutes = date.getMinutes() return `${hour < 10 ? '0' : ''}${hour}:${minutes < 10 ? '0' : ''}${minutes}` } export function formatDateTime (date: Date, locale: string): string { return `${formatDate(date, locale)} ${formatTime(date, locale)}` }