How to get weekday names using Intl?

I want to display in my i18n-ed application a list of 7 working days:

Sunday, Monday, Tuesday... Saturday

I rely on the Intl global object to format the date / time, but I cannot find an easy way to get only the names of the days of the week.

I thought I could add a few days to EPOCH to get to the first day of the week, but I can’t find the formatting print only on a weekday.

var date = new Date(0);
date.setDate(4 + day);
for (var i = 0; i < 7; i++) {
  var weekday = new Intl.DateTimeFormat(["en"], {
      weekday: "short" // ?? what should I put here
  }).format(date);
  console.log(weekday);
}

Conclusion:

Sunday, January 4, 1970
Monday, January 5, 1970
Tuesday, January 6, 1970
Wednesday, January 7, 1970
Thursday, January 8, 1970
Friday, January 9, 1970
Saturday, January 10, 1970

Required Conclusion:

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

I would also like to have a shorter version of days, for example Sun, Mon, Tue....

Alternatively, is there a way to get the day of the week strings from Intl? I tried to examine the object through the console, but I could not find them.

+4
source share
4

, Intl polyfill, { weekday: "short" } .

.

+3

, .

var weekday1 = weekday.split(",");
console.log(weekday1[0]);
0

Intl(also ergo Intl.js) does not have an API to get names on weekdays, so you have to hack requests for several days with the correct parameters. Perhaps this may add support for this in the future , but right now you're just out of luck. (At least if you didn’t manually add Intl.jsthe hotfix.)

0
source

Inspired from Olivine Solution

  /**
   * Get Weekdays
   *
   * Get weekdays names as an array
   *
   * @param {String} langauge
   * @param {String} short , long , narrow
   * @params {String} start defines in which day the weeks starts ex: Saturday , Sunday , ...
   *
   * @return {Array}
   */
  function getWeekdays (langauge, format, start) {

    var days = ["monday", "tuesday", "wednesday", "thursday", "friday", "faturday", "sunday"];

    var index = days.indexOf(start.toLowerCase());
    if (index < 0) {
      throw new Error('Invalid week day start :' + start);
    }

    var weekdays = [];
    for (var day = 5; day <= 11; day++) {
      weekdays.push(
        new Date(1970, 1 - 1, day + index).toLocaleString(langauge, { weekday: format })
      );
    }
    return weekdays;
  }

  // example of usage 
  getWeekdays('de','long','sunday');

  /* output : ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", 
              "Freitag", "Samstag"]
  */
0
source

All Articles