Javascript - Retrieve OS Date formatting

风流意气都作罢 提交于 2020-12-08 11:55:56

问题


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.

This is absolutely not the problem.

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.

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.

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


回答1:


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

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

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

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



来源:https://stackoverflow.com/questions/15834855/javascript-retrieve-os-date-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!