Home > front end >  Get list of days in the week that reflect locale in JavaScript?
Get list of days in the week that reflect locale in JavaScript?

Time:02-10

I have this function that returns days of the week:

var SEPARATOR = '  ';
function week_days(lang) {
    var result = [];
    for (var i = 0; i <= 6;   i) {
        var d = new Date(1970, 1, 1   i);
        result.push(d.toLocaleString(lang, {weekday: 'short'}).substring(0, 2));
    }
    return result.join(SEPARATOR);
}

console.log("en-us", week_days("en-us"))
console.log("pl-pl", week_days("pl-pl"))

but it's fixed and starts from Sunday like in the US locale. But in the Polish locale week starts from Monday. There are probably languages that use one or the other.

I can use new Date(1970, 1, 2 i); and it will work, but how I can make it based on Locale? (e.g. window.navigator.language).

CodePudding user response:

I've found that the spec has no way of knowing this information. It's tracked here:

https://github.com/tc39/ecma402/issues/6

This is an old issue. From the comments, it seems that there is a spec:

https://github.com/tc39/proposal-intl-locale-info

That should handle this.

  •  Tags:  
  • Related