Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Natively Format Javascript Dates & Numbers in different Locales (Including Persian Calendar)
(12345.6789).toLocaleString("fa-IR"); // ۱۲٬۳۴۵٫۶۷۹
var now = new Date();
now.toLocaleString(); // 1/27/2018, 12:07:03 AM
now.toLocaleDateString(); // 1/27/2018
now.toLocaleTimeString(); // 12:07:03 AM
now.toLocaleString("fa-IR") // ۱۳۹۶/۱۱/۷،‏ ۰:۰۷:۰۳
now.toLocaleDateString("fa-IR") // ۱۳۹۶/۱۱/۷
now.toLocaleTimeString("fa-IR") // ۰:۰۷:۰۳
var formatOptions = {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric'};
now.toLocaleString("en-US", formatOptions); // "Saturday, January 27, 2018"
now.toLocaleDateString("fa-IR", formatOptions); //۱۳۹۶ بهمن ۷, شنبه
/*
Syntax
toLocale...([locale [, options]]);
Locale
String or Array of Strings of locale code (language-COUNTRY) & two extra options for Numbering system & Calendar selection as below:
"nu": "arab", "arabext", "bali", "beng", "deva", "fullwide", "gujr", "guru", "hanidec", "khmr", "knda", "laoo", "latn", "limb", "mlym", "mong", "mymr", "orya", "tamldec", "telu", "thai", "tibt"
"ca": "gregory", "persian", "islamic", "islamicc", "iso8601", "buddhist", "chinese", "coptic", "ethioaa", "ethiopic", "hebrew", "indian", "japanese", "roc"
Options
Object with following params:
"weekday": "narrow", "short", "long", locale-specific by default
"month": "numeric", "2-digit", "narrow", "short", "long", locale-specific by default
"year", "day", "hour", "minute", "second": "numeric", "2-digit", locale-specific by default
"hour12": true, false, locale-specific by default
"timeZoneName": "short", "long", locale-specific by default
"localeMatcher": "lookup", "best fit" (default)
"formatMatcher": "basic", "best fit" (default)
Option Subsets
weekday, year, month, day, hour, minute, second
weekday, year, month, day
year, month, day
year, month
month, day
hour, minute, second
hour, minute
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment