Last active
July 27, 2019 21:44
-
-
Save ajmas/d8860840bd42089c72fe9f7b80d955b5 to your computer and use it in GitHub Desktop.
Display date in calendar locales
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat | |
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation | |
function formatField(text, length = 30) { | |
const textLen = text.length; | |
const padCount = length - textLen; | |
let newText = text + ' '; | |
for (let i=0; i<padCount; i++) { | |
newText += '.'; | |
} | |
return newText; | |
} | |
let now = new Date(); | |
let locale = navigator.language; | |
let options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', era: 'short }; | |
const calendars = ['buddhist', 'chinese', 'coptic', 'ethiopia', 'ethiopic', 'gregory', 'hebrew', | |
'indian', 'islamic', 'iso8601', 'japanese', 'persian', 'roc']; | |
calendars.forEach((calendar) => { | |
const dateTimeFormat = new Intl.DateTimeFormat(locale + '-u-ca-' + calendar, options); | |
console.log(formatField(calendar, 10) + ' ' + dateTimeFormat.format(now)); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment