Javascript - 检索操作系统日期格式

嗯...好吧,我已经在网上搜索了 2 天,但没有任何运气.我已经看到很多关于如何格式化 javascript 日期的答案,例如 new Date().toString("yyyy-MM-dd")... 这将返回类似 2013-04-05 的内容.

Hmmm... Okay so I've been searching the web for 2 days now without any luck. I've seen a lot of answers on how to format a javascript date for example new Date().toString("yyyy-MM-dd")... Which would return something like 2013-04-05.

这绝对不是问题.

我想要的是设置我的操作系统显示日期的格式,然后检索该特定格式并将其显示在浏览器中的可能性.

What I want, is the possibility to set the format in which my OS displays dates, then retrieve that specific format and display it in the browser.

例如,假设我将操作系统中的日期格式更改为 MM-yyyy/dd(这是为了争论,是否可行无关紧要).那么我希望在我的操作系统中看到 04-2013/05,对吧?接下来,我想通过 Javascript 在我的浏览器中检索这种特定格式,以便我可以使用它来格式化整个网页中的日期.

For example, let's say I changed the format of the date in my OS to MM-yyyy/dd (this is for arguement sakes, whether that would work or not is irrelevant)). Then I'd expect to see 04-2013/05 in my OS, right? Next I want to retrieve this specific format in my browser via Javascript so that I can use this to format my dates throughout my webpage.

如果这是疯了,无法做到,请告诉我,因为我在搜索中头疼.另外,如果您说使用 someDateObject.toLocaleDateString() 而没有确切解释为什么 .toLocaleDateString 会起作用,我会忽略它,因为我已经尝试将我的操作系统中的日期格式设置为多个格式,每次我使用 .toLocaleDateString() 时,我都会收到相同的格式:dd/MM/yyyy

If this is lunacy and cannot be done, please tell me, as I've got a headache from searching. Also, if you say use someDateObject.toLocaleDateString() without explaining exactly why .toLocaleDateString would work, I'm going to ignore it, because I've tried setting my date-format in my OS to numerous formats and every single time I use .toLocaleDateString(), I receive the same format: dd/MM/yyyy

推荐答案

.toLocaleDateString 方法的第一个属性使用的语言环境.

first attribute of .toLocaleDateString method locale(s) used.

您可以通过 navigator.language(在 firefox 或 chrome 中)参数获取当前语言环境.在 IE 中可以获取 navigator.browserLanguage 或 navigator.systemLanguage

current locale you can obtain through navigator.language (in firefox or chrome) parameter. in IE you can obtain navigator.browserLanguage or navigator.systemLanguage

在IE以外的浏览器中无法通过这种方式获取系统语言

in browsers other than IE it is impossible to obtain system language this way

之后,您可以调用 new Date.toLocaleString(navigator.language||navigator.browserLanguage),它将根据浏览器语言正确格式化

after this you can call new Date.toLocaleString(navigator.language||navigator.browserLanguage) and it will be formated correctly depending on browser language

相关文章