Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Retrieve OS Date formatting

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

like image 309
Deceased Avatar asked Apr 05 '13 13:04

Deceased


People also ask

How can I get current date in jquery in dd mm yyyy format?

get current date in jquery in dd/mm/yyyy format” Code Answer. var mm = String(today. getMonth() + 1).

What is JavaScript default date format?

The string format should be: YYYY-MM-DDTHH:mm:ss. sssZ , where: YYYY-MM-DD – is the date: year-month-day. The character "T" is used as the delimiter.


1 Answers

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

like image 120
Antharon Avatar answered Sep 27 '22 23:09

Antharon