// Due to some limitations of Date Internationalization API, this hacks are applied import PowerduckState from '../../app/powerduck-state'; import { capitalize } from '../extensions/string-extensions'; export default class DateLocalizationUtils { private static readonly DATE_FORMAT_FOR_RANGE_PICKER = (function () { // eslint-disable-next-line no-restricted-syntax const dummyDate = new Date(Date.UTC( 2022, 11, 20, )); const formatted = dummyDate.toLocaleDateString(PowerduckState.getCurrentLanguage(), { day: '2-digit', month: '2-digit', year: 'numeric', timeZone: 'UTC', }); return formatted.replace('20', 'DD').replace('12', 'MM').replace('2022', 'YYYY'); })(); private static readonly MONTH_IS_FIRST: boolean = (function () { // eslint-disable-next-line no-restricted-syntax const dummyDate = new Date(Date.UTC( 2024, 9, 20, )); try { const formatted = dummyDate.toLocaleDateString(PowerduckState.getCurrentLanguage(), { day: '2-digit', month: '2-digit', timeZone: 'UTC', }); const monthIndex = formatted.indexOf('10'); const dayIndex = formatted.indexOf('20'); return monthIndex < dayIndex; } catch (error) { return false; } })(); private static readonly PATTERN_WITHOUT_YEAR: string = (function () { // eslint-disable-next-line no-restricted-syntax const dummyDate = new Date(Date.UTC( 2024, 9, 30, )); try { const monthName = dummyDate.toLocaleDateString(PowerduckState.getCurrentLanguage(), { day: '2-digit', month: 'long', timeZone: 'UTC', }).split('30').join('').split('.').join('').trim(); const formatted = dummyDate.toLocaleDateString(PowerduckState.getCurrentLanguage(), { day: '2-digit', month: 'long', timeZone: 'UTC', }); return formatted.replace(monthName, '{{month}}').replace('30', '{{day}}'); } catch (error) { return '{{day}}. {{month}}'; } })(); private static readonly PATTERN_WITH_YEAR: string = (function () { // eslint-disable-next-line no-restricted-syntax const dummyDate = new Date(Date.UTC( 2024, 9, 30, )); try { const monthName = dummyDate.toLocaleDateString(PowerduckState.getCurrentLanguage(), { day: '2-digit', month: 'long', timeZone: 'UTC', }).split('30').join('').split('.').join('').trim(); const formatted = dummyDate.toLocaleDateString(PowerduckState.getCurrentLanguage(), { day: '2-digit', year: 'numeric', month: 'long', timeZone: 'UTC', }); return formatted.replace(monthName, '{{month}}').replace('30', '{{day}}').replace('2024', '{{year}}'); } catch (error) { return '{{day}}. {{month}}'; } })(); private static readonly PATTERN_PLACEHOLDERED: string = (function () { // eslint-disable-next-line no-restricted-syntax const dummyDate = new Date(Date.UTC( 2024, 9, 30, )); try { const monthName = dummyDate.toLocaleDateString(PowerduckState.getCurrentLanguage(), { month: 'long', timeZone: 'UTC', }); const formatted = dummyDate.toLocaleDateString(PowerduckState.getCurrentLanguage(), { year: 'numeric', month: 'long', timeZone: 'UTC', }); return formatted.replace(monthName, '{{date}}').replace('30', '{{day}}').replace('2024', '{{year}}'); } catch (error) { return '{{day}}. {{month}}'; } })(); static formatRange = (from: Temporal.PlainDateTime | Temporal.PlainDate, to: Temporal.PlainDateTime | Temporal.PlainDate): string => { if (from.year == to.year && from.month == to.month && from.day == to.day) { return this.getFormatted( from, this.PATTERN_WITH_YEAR, 'numeric', 'long', 'numeric', ); } else if (from.month == to.month) { // eslint-disable-next-line no-restricted-syntax const convertDate = new Date(Date.UTC( from.year, from.month - 1, from.day, )); convertDate.setDate(28); const monthName = convertDate.toLocaleDateString(PowerduckState.getCurrentLanguage(), { day: '2-digit', month: 'long', timeZone: 'UTC', }).split('28').join('').split('.').join('').trim(); return this.PATTERN_WITH_YEAR .replace('{{day}}', `${from.day} - ${to.day}`) .replace('{{month}}', monthName) .replace('{{year}}', from.year.toString()); } else if (from.year == to.year) { const fromDate = this.getFormatted( from, this.PATTERN_WITHOUT_YEAR, 'numeric', 'short', ); const toDate = this.getFormatted( to, this.PATTERN_WITHOUT_YEAR, 'numeric', 'short', ); const rangeVal = `${fromDate} - ${toDate}`; return this.PATTERN_PLACEHOLDERED.replace('{{date}}', rangeVal).replace('{{year}}', to.year.toString()); } else { const fromVal = this.getFormatted( from, this.PATTERN_WITH_YEAR, 'numeric', 'short', 'numeric', ); const toVal = this.getFormatted( to, this.PATTERN_WITH_YEAR, 'numeric', 'short', 'numeric', ); return `${fromVal} - ${toVal}`; } }; static getFormatedDateWithoutTime = ( dte: Temporal.PlainDateTime, showYear: boolean, day: 'numeric' | '2-digit' | undefined, month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow' | undefined, year?: 'numeric' | '2-digit' | undefined, ) => { if (showYear) { return this.getFormatted( dte, this.PATTERN_WITH_YEAR, day, month, year, ); } else { return this.getFormatted( dte, this.PATTERN_WITHOUT_YEAR, day, month, ); } }; private static getFormatted = ( dte: Temporal.PlainDateTime | Temporal.PlainDate, pattern: string, day: 'numeric' | '2-digit' | undefined, month?: 'numeric' | '2-digit' | 'long' | 'short' | 'narrow' | undefined, year?: 'numeric' | '2-digit' | undefined, ): string => { let monthVal: string; try { monthVal = dte.toLocaleString(PowerduckState.getCurrentLanguage(), { month, timeZone: 'UTC', })[capitalize](); } catch (error) { monthVal = dte.month.toString(); } let dayVal: string; if (day == '2-digit') { dayVal = dte.day.toString(); if (dayVal.length == 1) { dayVal = `0${dayVal}`; } } else { dayVal = dte.day.toString(); } if (year == null) { return pattern.replace('{{day}}', dayVal).replace('{{month}}', monthVal); } else { return pattern.replace('{{day}}', dayVal).replace('{{month}}', monthVal).replace('{{year}}', dte.year.toString()); } }; }