javascript 获取本地化的星期几

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10763761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:58:39  来源:igfitidea点击:

Getting localized day of week

javascriptdatelocalization

提问by Ry-

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:

我想在 JavaScript 中获取星期几的名称,本地化为用户的当前语言;最好使用比我现在使用的更好的东西:

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?

是否有捷径可寻?或者我是否只需要为尽可能多的语言环境提供日期字符串?

回答by vlk

I use Date.toLocaleString(), for example:

我使用Date.toLocaleString(),例如:

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

or

或者

d.toLocaleString('sk-SK', {weekday: 'short'});

回答by Ry-

Take a look at datejs, it handles localization very nicely. It comes with a lot of globalization setups. You just load the globalization setup of your current CultureInfo and datejs takes care of the rest.

看看 datejs,它很好地处理了本地化。它带有许多全球化设置。您只需加载当前 CultureInfo 的全球化设置,而 datejs 负责其余的工作。

Javascript Date Localization

Javascript 日期本地化