I would like to get the names of the days of the week in JavaScript localized in the current language of the user; preferably with something more enjoyable than what I am currently using:
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 do I just need to provide date strings for as many locales as possible?
I am using Date.toLocaleString() , for example:
Date.toLocaleString()
d = new Date(); d.toLocaleString(window.navigator.language, {weekday: 'long'});
or
d.toLocaleString('sk-SK', {weekday: 'short'});
Take a look at datejs, it does a great job of localization. It comes with many installations for globalization. You just load the globalization setting of your current CultureInfo, and datejs takes care of everyone else.
Javascript Date Localization