Skip to content

Instantly share code, notes, and snippets.

@ajmas
Last active July 27, 2019 21:44
Show Gist options
  • Save ajmas/d8860840bd42089c72fe9f7b80d955b5 to your computer and use it in GitHub Desktop.
Save ajmas/d8860840bd42089c72fe9f7b80d955b5 to your computer and use it in GitHub Desktop.
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