You can also use toLocaleString to format a date in JavaScript. Itβs easier to use toLocaleString than to create your own functions. Itβs more powerful too.
toLocaleString
toLocaleString takes in two parameters: locale and options.
const dateString = date.toLocaleString(locale, options)Letβs talk about options first.
options
By default, toLocaleString creates a string that looks like this: 1/3/2019, 11:10:10 PM. Itβs not very helpful.
options lets you define what goes into the created string.
Here are the possible properties you can use:
weekday:longproducesThursdayshortproducesThunarrowproducesT
year:numericproduces20192-digitproduces19
month:longproducesJanuaryshortproducesJannarrowproducesJnumericproduces12-digitproduces01
day:numericproduces32-digitproduces03
hour:numericproduces32-digitproduces03
minute:numericproduces32-digitproduces03
second:numericproduces32-digitproduces03
hour12: Iftrue, produces time in a 12h format; iffalse, produces time in 24h format. If omitted, defaults to how the locale creates time.timeZone: The timezone of the created string based on the IANA Timezone Database. (Check this Wikipedia article for an easy reference to the Timezone database).timeZoneName:longproduces the name of the timezone, likeSingapore Standard TimeshortproducesGMT +8
Every property mentioned above is optional. If you omit options, youβll get the default string.
const date = new Date(2019, 0, 3, 23, 10, 10)const string = date.toLocaleString('en-US')
console.log(string) // 1/3/2019, 11:10:10 PMIf you provide options, youβll create a string with the properties you defined. Properties that are left out will not be created in the result string.
In the example below, we produce the day of the week with weekday.
const weekdayOnly = { weekday: 'long' }const result = date.toLocaleString('en-US', weekdayOnly)
console.log(result) // ThursdayHereβs another example to produce year, month, and day information with year, month, and day properties.
const options = { year: 'numeric', month: 'long', day: '2-digit',}const result = date.toLocaleString('en-US', options)
console.log(result) // January 03, 2019locale
options tells toLocaleString what to include, locale tells toLocaleString how to create the string. It takes the cityβs date preferences into account.
For example, people in United States write dates in DD/MM/YYYY, while people in United Kingdom write dates in MM/DD/YYYY. The en-US and en-GB locales takes this into account.
const options = { year: 'numeric', month: '2-digit', day: '2-digit',}
const us = date.toLocaleString('en-US', options)const uk = date.toLocaleString('en-GB', options)
console.log(us) // 01/03/2019console.log(uk) // 03/01/2019locale also lets you create dates with different languages easily.
const options = { year: 'numeric', month: 'long', day: '2-digit', hour: 'numeric', minute: 'numeric', second: 'numeric',}
const chinese = date.toLocaleString('zh-Hans', options)const korean = date.toLocaleString('ko-KR', options)const danish = date.toLocaleString('da', options)
console.log(chinese) // 2019εΉ΄1ζ03ζ₯ δΈε11:10:10console.log(korean) // 2019λ
1μ 03μΌ μ€ν 11:10:10console.log(danish) // 03. januar 2019 11.10.10 PMYou can get a complete list of locales in the IANA Subtag Registry. (But I found the IANA Subtag Registry quite useless. I use this Stack Overflow post instead. You have to change _ to - to use the locales in JavaScript).
Other toLocaleString methods
JavaScript contains four methods to create a locale string. They are:
toLocaleString(which you learned in this lesson)toLocaleDateString(returns thedateportion fromtoLocaleString)toLocaleTimeString(returns thetimeportion fromtoLocaleString)Intl.DateTimeFormat(same astoLocaleString, but its syntax is slightly different)
Practice
Use toLocaleString to create the following date formats:
Thu, 03/01/2019January 20193 January 2019Jan 3 20192019-03-02
Welcome! Unfortunately, you donβt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson