user592501
user592501

Reputation:

Setting Moment.js default date format to dd/mm/yyyy

When calling .calendar() in Moment.js, dates are output using the MM/DD/YYYY format. From looking at the code, this appears to be controlled by the L token. Likewise, the LL token outputs the date as November 29 2013.

The only problem is, these are US date formats and I need to display mine according to Australian standards, e.g. DD/MM/YYYY and 29 November 2013.

Does anyone know where/ how to set this?

NB: My system locale and location are set to English (Australia) and Australia respectively.

Upvotes: 22

Views: 51689

Answers (3)

Anto Subash
Anto Subash

Reputation: 3215

You have to use the moment-with-lang.js and set the language for "Australia"

check the docs here http://momentjs.com/docs/#/i18n/changing-locale/

for you it should be something like moment.lang('en-AU');

Upvotes: 13

awe
awe

Reputation: 22442

From version 2.8.1 and newer, use locale:

moment.locale('en-AU');

If you are using a version older than 2.8.1, use lang:

moment.lang('en-AU');

This will set the locale globally in moment, and all the locale-dependents functions will be affected after setting this.

Note that this is not available prior to version 1.7.0.

See documentation.

Upvotes: 9

arturomp
arturomp

Reputation: 29590

Using moment-with-lang.js , the code

<script src="moment-with-langs.js"></script>
time = moment("Dec 25, 1995");
moment(time).calendar() // note the absence of moment.lang("en-AU");

outputs

25/12/1995 

In contrast, using moment.lang("en-AU"); we get that

moment.lang("en-AU");
moment(time).calendar();

outputs

25/12/1995 

You can also hardcode the desired output, but using format()

moment(time).format("D MMMM YYYY")

outputs

25 December 1995

Upvotes: 11

Related Questions