Currency and date formatting based on locale

Loading

Currency and date formatting are crucial for providing a user-friendly experience on websites and applications, especially when catering to a global audience. Different regions have distinct conventions for displaying currency, dates, and times, which can significantly impact usability. To accommodate users from various locales, developers need to ensure that the formatting is dynamic and adjusts based on the user’s location or language settings.

This guide covers how to format currency and date values correctly based on the locale using JavaScript and relevant APIs.


Step 1: Detecting User Locale

Before formatting the currency and date, it’s essential to detect the user’s locale or language preference. You can do this using the navigator.language property in JavaScript, which provides the user’s default language.

Example:

const userLang = navigator.language || navigator.languages[0];
console.log(userLang); // Outputs language code, e.g., "en-US", "fr-FR", "de-DE"

In addition to this, you can detect the user’s region by extracting the country code from the language string (e.g., en-US or fr-FR), which helps in determining the appropriate currency and date formatting.


Step 2: Formatting Currency Based on Locale

To format currency dynamically based on the user’s locale, JavaScript’s Intl.NumberFormat API is an excellent tool. This API allows you to format numbers as currencies with the appropriate currency symbol and thousands separators.

Here’s how to use it:

  1. Basic Currency Formatting:

The Intl.NumberFormat constructor can be used with the style option set to currency to format numbers as currency.

Example:

const amount = 123456.789;
const userLocale = navigator.language || 'en-US'; // Use user's language setting

const formattedCurrency = new Intl.NumberFormat(userLocale, {
style: 'currency',
currency: 'USD', // You can dynamically change this based on the user's region
}).format(amount);

console.log(formattedCurrency); // Outputs: "$123,456.79" (for en-US)
  1. Currency Formatting with Dynamic Currency Codes:

To detect the appropriate currency symbol for a given locale, you can set the currency parameter dynamically. For instance, if the locale is en-GB, the currency will be GBP (Pounds), while for en-US, it will be USD (Dollars).

Example:

const locale = navigator.language; // Automatically detect the locale
const currency = {
'en-US': 'USD',
'en-GB': 'GBP',
'fr-FR': 'EUR',
'de-DE': 'EUR',
};

const amount = 123456.789;

const formattedCurrency = new Intl.NumberFormat(locale, {
style: 'currency',
currency: currency[locale] || 'USD', // Fallback to USD
}).format(amount);

console.log(formattedCurrency); // Outputs: "$123,456.79" (for en-US) or "£123,456.79" (for en-GB)

Step 3: Formatting Dates Based on Locale

JavaScript’s Intl.DateTimeFormat API is used to format dates and times according to the locale. This API automatically adjusts the format based on the user’s region, ensuring that the date and time are presented in a familiar format.

  1. Basic Date Formatting:

Here’s how to format dates based on the user’s locale:

const today = new Date();
const userLocale = navigator.language || 'en-US';

const formattedDate = new Intl.DateTimeFormat(userLocale).format(today);

console.log(formattedDate); // Outputs date formatted for the user's locale, e.g., "4/16/2025" for en-US or "16/04/2025" for en-GB
  1. Custom Date Formats:

You can specify options to customize the date formatting further. For example, to include day, month, year, and time:

const today = new Date();
const userLocale = navigator.language || 'en-US';

const customDateFormat = new Intl.DateTimeFormat(userLocale, {
weekday: 'long', // "Monday"
year: 'numeric', // "2025"
month: 'long', // "April"
day: 'numeric', // "16"
}).format(today);

console.log(customDateFormat); // Outputs: "Thursday, April 16, 2025" for en-US
  1. Time Formatting:

You can also format the time separately based on the user’s locale:

const time = new Date();

const formattedTime = new Intl.DateTimeFormat(userLocale, {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: true, // 12-hour format (can be false for 24-hour format)
}).format(time);

console.log(formattedTime); // Outputs: "05:15:30 PM" for en-US or "17:15:30" for de-DE

Step 4: Combining Currency and Date Formatting

For an optimal user experience, it’s common to display both currency values and dates on the same page or in the same application. Here’s how you can combine both formatting techniques to handle dynamic content:

Example:

const amount = 98765.4321;
const today = new Date();
const userLocale = navigator.language || 'en-US';

const currencyFormatted = new Intl.NumberFormat(userLocale, {
style: 'currency',
currency: 'USD',
}).format(amount);

const dateFormatted = new Intl.DateTimeFormat(userLocale, {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
}).format(today);

console.log(`The amount is ${currencyFormatted} and today's date is ${dateFormatted}`);

This will output a message like:

The amount is $98,765.43 and today's date is Thursday, April 16, 2025

Step 5: Handling Different Regional Formats

In some regions, the same language may use different date or currency formats. For example, both en-GB (United Kingdom) and en-US (United States) use English but with different conventions for formatting dates and currencies.

To handle this, you can create a mapping based on the locale to ensure the correct currency and date format for each region:

const localeFormats = {
'en-US': { currency: 'USD', date: { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' } },
'en-GB': { currency: 'GBP', date: { year: 'numeric', month: 'numeric', day: 'numeric' } },
'fr-FR': { currency: 'EUR', date: { day: 'numeric', month: 'numeric', year: 'numeric' } },
};

const userLocale = navigator.language || 'en-US';
const { currency, date } = localeFormats[userLocale] || localeFormats['en-US'];

const amount = 12345.67;
const today = new Date();

const formattedCurrency = new Intl.NumberFormat(userLocale, {
style: 'currency',
currency: currency,
}).format(amount);

const formattedDate = new Intl.DateTimeFormat(userLocale, date).format(today);

console.log(`The amount is ${formattedCurrency} and today's date is ${formattedDate}`);

Step 6: Fallbacks for Unsupported Locales

If the user’s locale is unsupported or unavailable, it’s essential to have a fallback to ensure your website or application displays content in a default format.

const userLocale = navigator.language || 'en-US';
const supportedLocales = ['en-US', 'fr-FR', 'de-DE'];

if (!supportedLocales.includes(userLocale)) {
console.log('Using default locale (en-US)');
// Fallback to a default locale like en-US
}

Leave a Reply

Your email address will not be published. Required fields are marked *