Initial commit
This commit is contained in:
31
packages/i18n/src/format.ts
Normal file
31
packages/i18n/src/format.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
Reference in New Issue
Block a user