JavaScript - Get system short date format

Is there any way to get system short date format in JavaScript?

For example whether system's short date is in American format eg. m/d/Y or in european eg. d/m/Y

Please note: This is not question about formatting date or calculating it based on geolocation, but about getting the format from the OS/system


Solution 1:

After a pinch of research I concluded that technically it's not possible to get regional settings -and by this, date format- but you can do several other things. Pick one of these options:
a) The already mentioned -and outdated- "toLocaleString()" function:

var myDate = new Date(1950, 01, 21, 22, 23, 24, 225);
var myDateFormat = myDate.toLocaleString();
alert (myDateFormat);

ISSUES:
1) You can't "myDateFormat.replace" to get the date mask as month is not stored as "01", "02", etc in the string but as text instead, based on locale (like "February" in English but it's "Φεβρουάριος" in Greek and who knows what in e.g. Klingon).
2) Different behavior on different browsers
3) Different behavior on different OS and browser versions...

b) Use the toISOString() function instead of toLocaleString(). You won't get the locale date mask but get a date from which you can tell where's which part of the date (ie where "month" or "day" is in that string). You can also work with getUTCDate(), getUTCMonth() and getUTCDay() functions. You still can't tell what date format the client uses, but can tell which Year/Month/Day/etc you work with when you grab a date; use the code above to test the functions I mentioned here to see what you can expect.

c) Read Inconsistent behavior of toLocaleString() in different browser article and use the (IMHO great) solution described there

Solution 2:

for my case i used a custom date that i know what number is day, what is month and what is year so it can possible with a simple replace statement.

let customDate = new Date(2222, 11, 18);
let strDate = customDate.toLocaleDateString();
let format = strDate
    .replace("12", "MM")
    .replace("18", "DD")
    .replace("2222", "yyyy");