
This ad doesn't have any photos.
|
JavaScript’s tolocalestring locales list is a powerful way to format dates, numbers, and currency according to different language and regional settings. This method provides developers with the flexibility to present data in a user-friendly manner, ensuring better localization and readability across different regions.
In this article, we will explore how toLocaleString() works, how to specify different locales, and a list of commonly supported locales to help developers format their data appropriately.
What is toLocaleString()? The toLocaleString() method is available on JavaScript’s Date and Number objects. It allows for formatting dates and numbers based on a specified locale, or it defaults to the user’s system locale if none is provided.
Syntax: javascript Copy Edit date.toLocaleString([locales[, options]]) number.toLocaleString([locales[, options]]) locales: A string or array of BCP 47 language tags (e.g., "en-US", "fr-FR"). options: An optional object that customizes the output format (e.g., date styles, currency, or numbering system). Using toLocaleString() for Date Formatting javascript Copy Edit const date = new Date(); console.log(date.toLocaleString("en-US")); // Output: "3/8/2025, 12:45:30 PM" console.log(date.toLocaleString("fr-FR")); // Output: "08/03/2025, 12:45:30" By changing the locale, the date format adapts to different conventions used in those regions.
Using toLocaleString() for Number Formatting javascript Copy Edit const number = 1234567.89; console.log(number.toLocaleString("de-DE")); // Output: "1.234.567,89" console.log(number.toLocaleString("ja-JP")); // Output: "1,234,567.89" Here, the method applies region-specific number separators and decimal points.
Locales List for toLocaleString() JavaScript supports a wide range of locales, including but not limited to:
Common Locales for English Variants "en-US" – United States "en-GB" – United Kingdom "en-CA" – Canada "en-IN" – India "en-AU" – Australia Common Locales for European Languages "fr-FR" – French (France) "de-DE" – German (Germany) "es-ES" – Spanish (Spain) "it-IT" – Italian (Italy) "nl-NL" – Dutch (Netherlands) Asian Language Locales "ja-JP" – Japanese "zh-CN" – Chinese (Simplified, China) "ko-KR" – Korean "hi-IN" – Hindi (India) Middle Eastern and Other Locales "ar-SA" – Arabic (Saudi Arabia) "he-IL" – Hebrew (Israel) "ru-RU" – Russian (Russia) "pt-BR" – Portuguese (Brazil) Conclusion The toLocaleString() method is an essential tool for JavaScript developers looking to present dates and numbers in a localized format. By understanding the supported locales and how they affect
|