Get list of days in the week that reflect locale in JavaScript?

Viewed 109

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).

1 Answers
Related