Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Display date in calendar locales
// 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
You can’t perform that action at this time.