Arafangion Arafangion - 9 months ago 49
Javascript Question

Using moment.js, how to display the current date format for the user?

Given a text field, I want to have a suitable placeholder. A typical placeholder will be something like: "mm/dd/yyyy".

However, I would like to use locale-aware dates using moment.js.

This means that I will be specifying "l" as the moment.js date format, howe do I determine the date format that moment.js will be using in this case?

The user will not understand what "l" means, so using this value in the placeholder text makes very little sense.

Specifically, I am hoping to be able to access something like moment's internal "defaultLongDateFormat". (Though that is merely a default - moment.js probably updates it or has some other mapping at runtime for locale-aware date formats - I would like to access that mapping.)


There are multiple downvotes (who aren't explaining why they're downvoting it).

I think this is because they arent' understanding the question, so here are some examples:

I want a function such that:
getFormat("l") -> "mm/dd/yyyy", or equivalent for the US locales.
getFormat("l") -> "dd/mm/yyyy", or equivalent, for the AU locales.

I do not want to format a given date, or to parse a given date - I merely want to determine it's user-friendly format given an arbitruary moment.js format, specifically, for 'l'.

Answer Source

I don't think it's exposed nicely, but if the browser has its language configured correctly you can do something like this:

var lang = navigator.languages ? navigator.languages : navigator.language;


Languages behave slightly differently depending on which browser you're using, so don't know how reliable this is.