Files
iptvnator/libs/ui/pipes/src/lib/date-format.util.ts
4gray c97673b928 refactor: refactor date handling across components to use date-fns and Intl.DateTimeFormat
- Replaced moment.js usage with Intl.DateTimeFormat for date formatting in various components.
- Introduced utility functions `normalizeDateLocale` and `formatWithIntl` for locale normalization and date formatting.
- Updated PlaylistSwitcher, RecentPlaylists, ContentCard, EPG components, and MultiEpgContainer to utilize the new date formatting approach.
- Removed moment-date pipe and related tests, simplifying date handling logic.
- Ensured all date displays respect the current locale settings.

Entire-Checkpoint: 271bd0162027
2026-04-11 10:41:33 +02:00

53 lines
1.3 KiB
TypeScript

const DEFAULT_DATE_LOCALE = 'en';
const DATE_LOCALE_ALIASES: Record<string, string> = {
ary: 'ar-MA',
by: 'be',
zhtw: 'zh-Hant',
};
const formatterCache = new Map<string, Intl.DateTimeFormat>();
export function normalizeDateLocale(locale?: string | null): string {
const normalizedLocale = String(locale ?? '').trim();
if (!normalizedLocale) {
return DEFAULT_DATE_LOCALE;
}
return (
DATE_LOCALE_ALIASES[normalizedLocale.toLowerCase()] ?? normalizedLocale
);
}
export function formatWithIntl(
value: Date | number | string,
options: Intl.DateTimeFormatOptions & {
locale?: string | null;
timeZone?: string;
} = {}
): string {
const date =
value instanceof Date
? value
: typeof value === 'number'
? new Date(value)
: new Date(value);
if (Number.isNaN(date.getTime())) {
return '';
}
const { locale, ...formatOptions } = options;
const resolvedLocale = normalizeDateLocale(locale);
const cacheKey = JSON.stringify([resolvedLocale, formatOptions]);
let formatter = formatterCache.get(cacheKey);
if (!formatter) {
formatter = new Intl.DateTimeFormat(resolvedLocale, formatOptions);
formatterCache.set(cacheKey, formatter);
}
return formatter.format(date);
}