Momentjs: format date keeping local day, month and year
I am using Moment.js to format some dates in a "country specific" order using the built-in locale . So, for example, this code will print the date specified in the Netherlands by default:
moment.locale('nl');
nlDate = moment();
nlDate.format('ll'); // "12 nov. 2014"
A different language menu will print a different order:
moment.locale('en');
nlDate = moment();
nlDate.format('ll'); // "Nov 12, 2014"
What I would like to do is change the format of the output string while preserving the order of the locale; eg,
(nl) 12 nov. 2014 --> 12-11-'14
(en) Nov 12, 2014 --> 11-12-'14
Unfortunately custom format , I can't keep the local 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 refer to my efforts here and here , but not sure if I'm in the right direction.
Thanks, Luca
+3
source to share
2 answers
Setting the language options 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 this is the best I have found and I am very happy with it.
0
source to share