7

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

Deceased
  • 198
  • 1
  • 9
  • Please check the `.toLocaleString` methods. Have you had a look here http://stackoverflow.com/questions/85116/how-do-i-display-a-date-time-in-the-users-locale-format-and-time-offset or here http://stackoverflow.com/questions/2388115/get-locale-short-date-format-using-javascript – Mark Walters Apr 05 '13 at 13:18
  • 2
    Have seen them both... Have you tested them, I know for a fact the answer by "mwrf" on the http://stackoverflow.com/questions/2388115/get-locale-short-date-format-using-javascript page, which seems to be the most complete didn't solve my problem... thank you for trying though – Deceased Apr 05 '13 at 13:34

1 Answers1

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

Antharon
  • 49
  • 1
  • 1
    I get what you're saying, and it makes sense, with this you are able to force a locality, eg: "de-DE"... but this is not what I'm looking for. I used the radical example of MM-yyyy/dd to demonstrate that I do not necessarily want a known locality. What I need is the date format that the user specified in his/her OS. I really do appreciate the help though. – Deceased Apr 05 '13 at 13:53
  • if user uses IE, it is possible throught navigator.systemLanguage variable, in the other cases you can still guess, that language used is same as language of his browser, or even ask him and save this property into cookie – Antharon Apr 05 '13 at 14:19
  • 1
    Hmmm... I've tried the navigator.systemLanguage just now, although a neat one, it is still not the trick I'm looking for. See, I know how to get/set the locale, but what I want to do is get/set the personalized date format that the user set into their machine. I'd like to do this without having them set it again in my web-app. What I want is for the web-app to pick up the date format e.g. MM-yyyy/dd and use that to display dates throughout the rest of the app. – Deceased Apr 05 '13 at 14:37
  • @Deceased and while that makes a lot of sense, it seems like there simply still isn't a way to do that. Anatharon's answer is the best you can get and the way Edge does it now. Most other browsers (Google Chrome, Firefox etc.) take the browser language instead but the principle stays the same. – Despacito 2 Jan 07 '22 at 13:24