How to get the weekday names using Intl?

 const weekdayDateMap = {
  Mon: new Date('2020-01-06T00:00:00.000Z'),
  Tue: new Date('2020-01-07T00:00:00.000Z'),
  Wed: new Date('2020-01-08T00:00:00.000Z'),
  Thu: new Date('2020-01-09T00:00:00.000Z'),
  Fri: new Date('2020-01-10T00:00:00.000Z'),
  Sat: new Date('2020-01-11T00:00:00.000Z'),
  Sun: new Date('2020-01-12T00:00:00.000Z'),
};
const shortWeekdays = Object.keys(weekdayDateMap);

const getDayOfWeek = (shortName, locale = 'en-US', length = 'short') =>
  new Intl.DateTimeFormat(locale, { weekday: length }).format(weekdayDateMap[shortName]);

const getDaysOfWeek = (locale = 'en-US', length = 'short') =>
  shortWeekdays.map(shortName => getDayOfWeek(shortName, locale, length));


console.log(getDayOfWeek('Mon', 'de-DE')) // "Mo"

console.log(getDaysOfWeek('de-DE')) // ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"]

Note:

If you're calling the Intl.DateTimeFormat formatter many times a second, it's more efficient to create the formatter once and re-use it.


I was misled because I was using the Intl polyfill, which does not support yet { weekday: "short" } as option.

Using native Intl implementations works as expected.


my solution in 2021 with es6

/**
 * Return list of days
 * 🌍 localeName : name of local, f.e. en-GB, default es-MX
 *  ✅ weekday   : formart of weekday short/long (Default)
 */
function daysForLocale(localeName = 'es-MX', weekday = 'long') {
  const format = new Intl.DateTimeFormat(localeName, { weekday }).format;
  return [...Array(7).keys()]
    .map((day) => format(new Date(Date.UTC(2021, 5, day))));
}

// ##############################################################
// testing daysForLocale function
// ##############################################################
console.log(daysForLocale());
// ['domingo','lunes',...,'viernes','sábado']
console.log(daysForLocale('en-GB', 'short'));
// ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri','Sat']
console.log(daysForLocale('ja-JP', 'short'));
// ['日', '月', '火','水', '木', '金','土']