lzzluca
lzzluca

Reputation: 806

moment.js: format a date keeping the locale's day, month and year order

I am using Moment.js to format some dates in a "country specific" order, using the native locale feature. So, for example, this code will print a date ordered in the Netherlands's default:

moment.locale('nl');
nlDate = moment();
nlDate.format('ll'); // "12 nov. 2014"

A different locale setting will print a different order:

moment.locale('en');
nlDate = moment();
nlDate.format('ll'); // "Nov 12, 2014"

What I would like to do is to change the format of the output string, keeping the locale order; so for example:

(nl) 12 nov. 2014   -->   12-11-'14
(en) Nov 12, 2014   -->   11-12-'14

Sadly, by a custom format, I am not able to keep the locale order:

nlDate = moment();

nlDate.locale('nl');
nlDate.format("DD-MM-'YY"); // "12-11-'14"

nlDate.locale('en');
nlDate.format("DD-MM-'YY"); // "12-11-'14"

From the above I would like to get:

(nl) 11-12-'14
(en) 12-11-'14

Any help?

I am addressing my effort here and here but not sure I am in the right direction.

Thank you, Luca

Upvotes: 3

Views: 3449

Answers (2)

lzzluca
lzzluca

Reputation: 806

Customize the language settings seems to work pretty well:

moment.locale('en', {
    longDateFormat : {
        LT: "h:mm A",
        L: "MM-DD-'YY",
        LL: "MMMM Do YYYY",
        LLL: "MMMM Do YYYY LT",
        LLLL: "dddd, MMMM Do YYYY LT"
    }
});
moment.locale("en");
moment().format("L"); // 11-12-'14

So far is the best I have found and I am pretty happy with it.

Upvotes: 0

Mohammed Safeer
Mohammed Safeer

Reputation: 21535

Try this,

moment.locale("nl").format('L');

and

moment.locale("en").format('L');

Upvotes: 4

Related Questions