JavaScript – Get system short date format

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);

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

Leave a Comment