The Date type's toLocaleString()
method returns the date and time in a format appropriate for the locale.
The format includes AM or PM for the time and doesn't include any time-zone information.
dateObj.toLocaleString([locales[, options]])
options Legal properties:
Option | Legal values: |
---|---|
dateStyle | "full" "long" "medium" "short" |
timeStyle | "full" "long" "medium" "short" |
localeMatcher | "best-fit" (default) "lookup" |
timeZone | Empty |
hour12 | false true |
hourCycle | "h11" "h12" "h23" "h24" |
formatMatcher "basic" "best-fit" (default) weekday "long" "short" "narrow" year "2-digit" "numeric" month "2-digit" "long" "narrow" "numeric" "short" day "2-digit" "long" | |
hour minute | "2-digit" "long" "2-digit" "long" |
second | "2-digit" "long" |
timeZoneName | "long" "short" |
toLocaleString()
without arguments depends on the implementation, the default locale, and the default time zone
var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));
console.log(date.toLocaleString());
This example shows some of the variations in localized date and time formats.
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); // America/Los_Angeles for the US // US English uses month-day-year order and 12-hour time with AM/PM console.log(date.toLocaleString('en-US')); // British English uses day-month-year order and 24-hour time without AM/PM console.log(date.toLocaleString('en-GB')); // Korean uses year-month-day order and 12-hour time with AM/PM console.log(date.toLocaleString('ko-KR')); // Arabic /*w w w .ja va 2 s . co m*/ console.log(date.toLocaleString('ar-EG')); // for Japanese, console.log(date.toLocaleString('ja-JP-u-ca-japanese'));
The results provided by toLocaleString()
can be customized using the options argument:
var date = new Date(Date.UTC(2020, 10, 20, 3, 0, 0)); // request a weekday along with a long date var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; console.log(date.toLocaleString('de-DE', options)); // an application may want to use UTC options.timeZone = 'UTC'; options.timeZoneName = 'short'; console.log(date.toLocaleString('en-US', options)); // the US needs 24-hour time console.log(date.toLocaleString('en-US', { hour12: false }));
This example demonstrates all legal language codes.
var n = new Date(); console.log("ar-SA: " + n.toLocaleString("ar-SA")); console.log("bn-BD: " + n.toLocaleString("bn-BD")); console.log("bn-IN: " + n.toLocaleString("bn-IN")); console.log("cs-CZ: " + n.toLocaleString("cs-CZ")); console.log("da-DK: " + n.toLocaleString("da-DK")); console.log("de-AT: " + n.toLocaleString("de-AT")); console.log("de-CH: " + n.toLocaleString("de-CH")); console.log("de-DE: " + n.toLocaleString("de-DE")); console.log("el-GR: " + n.toLocaleString("el-GR")); console.log("en-AU: " + n.toLocaleString("en-AU")); console.log("en-CA: " + n.toLocaleString("en-CA")); console.log("en-GB: " + n.toLocaleString("en-GB")); console.log("en-IE: " + n.toLocaleString("en-IE")); console.log("en-IN: " + n.toLocaleString("en-IN")); console.log("en-NZ: " + n.toLocaleString("en-NZ")); console.log("en-US: " + n.toLocaleString("en-US")); console.log("en-ZA: " + n.toLocaleString("en-ZA")); console.log("es-AR: " + n.toLocaleString("es-AR")); console.log("es-CL: " + n.toLocaleString("es-CL")); console.log("es-CO: " + n.toLocaleString("es-CO")); console.log("es-ES: " + n.toLocaleString("es-ES")); console.log("es-MX: " + n.toLocaleString("es-MX")); console.log("es-US: " + n.toLocaleString("es-US")); console.log("fi-FI: " + n.toLocaleString("fi-FI")); console.log("fr-BE: " + n.toLocaleString("fr-BE")); console.log("fr-CA: " + n.toLocaleString("fr-CA")); console.log("fr-CH: " + n.toLocaleString("fr-CH")); console.log("fr-FR: " + n.toLocaleString("fr-FR")); console.log("he-IL: " + n.toLocaleString("he-IL")); console.log("hi-IN: " + n.toLocaleString("hi-IN")); console.log("hu-HU: " + n.toLocaleString("hu-HU")); console.log("id-ID: " + n.toLocaleString("id-ID")); console.log("it-CH: " + n.toLocaleString("it-CH")); console.log("it-IT: " + n.toLocaleString("it-IT")); console.log("jp-JP: " + n.toLocaleString("jp-JP")); console.log("ko-KR: " + n.toLocaleString("ko-KR")); console.log("nl-BE: " + n.toLocaleString("nl-BE")); console.log("nl-NL: " + n.toLocaleString("nl-NL")); console.log("no-NO: " + n.toLocaleString("no-NO")); console.log("pl-PL: " + n.toLocaleString("pl-PL")); console.log("pt-BR: " + n.toLocaleString("pt-BR")); console.log("pt-PT: " + n.toLocaleString("pt-PT")); console.log("ro-RO: " + n.toLocaleString("ro-RO")); console.log("ru-RU: " + n.toLocaleString("ru-RU")); console.log("sk-SK: " + n.toLocaleString("sk-SK")); console.log("sv-SE: " + n.toLocaleString("sv-SE")); console.log("ta-IN: " + n.toLocaleString("ta-IN")); console.log("ta-LK: " + n.toLocaleString("ta-LK")); console.log("th-TH: " + n.toLocaleString("th-TH")); console.log("tr-TR: " + n.toLocaleString("tr-TR")); console.log("zh-CN: " + n.toLocaleString("zh-CN")); console.log("zh-HK: " + n.toLocaleString("zh-HK")); console.log("zh-TW: " + n.toLocaleString("zh-TW"));