Files
dwellops-platform/packages/i18n/src/format.ts
2026-03-10 21:30:52 -03:00

32 lines
920 B
TypeScript

/**
* Formats a date using the Intl.DateTimeFormat API.
*
* @param date - The date to format.
* @param locale - The locale to use.
* @param options - Optional Intl.DateTimeFormatOptions.
* @returns The formatted date string.
*/
export function formatDate(
date: Date | string,
locale: string,
options?: Intl.DateTimeFormatOptions,
): string {
const d = typeof date === 'string' ? new Date(date) : date;
return new Intl.DateTimeFormat(locale, options).format(d);
}
/**
* Formats a number as a currency string.
*
* @param amount - The amount to format.
* @param currency - The ISO 4217 currency code.
* @param locale - The locale to use.
* @returns The formatted currency string.
*/
export function formatCurrency(amount: number, currency: string, locale: string): string {
return new Intl.NumberFormat(locale, {
style: 'currency',
currency,
}).format(amount);
}