Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

EDIT:

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'.

like image 495
Arafangion Avatar asked Jul 13 '15 01:07

Arafangion


People also ask

How can I get current date in moment?

To get current date with Moment and JavaScript, we use the moment function without any arguments. const datetime = moment(); to call moment to get a moment object with the current date.

How can I get date in dd mm yyyy format in moment?

SearchDate = moment(new Date(), "DD/MM/YYYY");


2 Answers

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;

moment().locale(lang).localeData()._longDateFormat['L']

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

like image 72
Adam R Avatar answered Oct 19 '22 02:10

Adam R


Follow up to Adam R's answer:

Seems to have been exposed by now:

localeData.longDateFormat(dateFormat);

returns the full format of abbreviated date-time formats LT, L, LL and so on

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

Get the currently used locale data by moment.localeData()

like image 22
fabianm Avatar answered Oct 19 '22 01:10

fabianm