The Intl new adding to ECMAScript Internationalization API, has made a lot easier for developer to formatting and comparison of date, number and show on. In this blog will only talk date and its uses.
new Date() has been used in code so when you will run it, result will different but format will remain same.
Localisation of Date
new Date().toLocaleDateString('en-IN', {
month: 'long',
day: '2-digit',
year: 'numeric'
})
// 15 August 2021
Lets convert it into Spanish
new Date().toLocaleDateString('es', {
month: 'long',
day: '2-digit',
year: 'numeric'
})
// 15 de agosto de 2021
Just one change made it to show in Spanish, there are numbers of language it supports.
Time
new Date().toLocaleTimeString('en-IN', {
hour: 'numeric',
minute: 'numeric',
timeZoneName: 'short',
hour12: false
})
// 20:41 IST
We have addd timezone and asked to only show 24 hour format. You can also include timeZoneName
short and long.
Let get the time in 12 hours format
new Date().toLocaleTimeString('en-IN', {
hour: 'numeric',
minute: 'numeric'
})
// 8:43 pm
By default Intl will show 12 hour format.
Get Weekday
new Date().toLocaleTimeString('en-IN', {
hour: 'numeric',
minute: 'numeric',
weekday: 'long'
})
// Sunday, 8:49 pm
Timezone
Get time of CET, you can pass any timezone to get time.
new Date().toLocaleTimeString('en-IN', {
hour: 'numeric',
minute: 'numeric',
timeZone: 'CET'
})
// 5:46 pm
Top comments (0)