Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting localized day of week

Tags:

I'd like to get the names of the days of the weeks in JavaScript, localized to the user's current language; preferably with something a bit nicer than what I'm using now:

var weekDays = [];
var d = new Date();

while(d.getDay() > 0) {
    d.setDate(d.getDate() + 1);
}

while(weekDays.length < 7) {
    weekDays.push(d.toLocaleDateString().match(/\w+/)[0]);
    d.setDate(d.getDate() + 1);
}

Is there an easy way to do this? Or am I just going to have to provide date strings for as many locales as I can?

like image 843
Ry- Avatar asked May 26 '12 05:05

Ry-


1 Answers

I use Date.toLocaleString(), for example:

d = new Date();
d.toLocaleString(window.navigator.language, {weekday: 'long'});

or

d.toLocaleString('sk-SK', {weekday: 'short'});
like image 138
vlk Avatar answered Nov 17 '22 21:11

vlk