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

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

However, I would like to use dates corresponding to the language values ​​using the moment.js function.

This means that I will specify “l” as the date.js format, but can I define a date format that will use the .js moment in this case?

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

In particular, I hope to access something like the internal "defaultLongDateFormat". (Although this is just the default value - moment.js probably updates it or has some other run-time mapping for the date formats available for the locale - I would like to access this mapping.)

EDIT:

There are several downvotes (which do not explain why they reduce it).

I think this is because they do not understand the question, so here are a few examples:

I need a function such that: getFormat ("l") → "mm / dd / yyyy" or the equivalent for US locales. getFormat ("l") → "dd / mm / yyyy" or the equivalent for AU locales.

I don’t want to format the given date or analyze a specific date - I just want to define it in a user-friendly format, taking into account the format last.js in the format of arguary, in particular, for “l”.

+5
source share
2 answers

I do not think this is clearly visible, but if the browser has correctly configured the language, you can do something like this:

var lang = navigator.languages ? navigator.languages : navigator.language; moment().locale(lang).localeData()._longDateFormat['L'] 
Languages

Languages ​​behave somewhat differently, depending on which browser you use, so you don’t know how reliable it is.

+4
source

Follow Adam R's answer:

It seems now it has been exposed:

localeData.longDateFormat(dateFormat);

returns the full format of the abbreviated date and time formats LT, L, LL, etc.

(source: http://momentjs.com/docs/ )

Get current locale data using moment.localeData()

+4
source

All Articles