Reputation: 15806
My application sends an HTML file with javascript like this:
$(function () {
moment.locale('fr');
$('#datetimepicker9').datetimepicker({
viewMode: 'years',
locale: 'fr',
format: '' /* <========= problem! */
});
});
With moment, when I set to a locale, is there a way to get the short date format of the configuration like "'j F Y'
" for fr
?
I found it but it's hack-ish:
moment()['_locale']['_longDateFormat']['L']
So my code now:
$(function () {
moment.locale('fr');
$('#datetimepicker9').datetimepicker({
viewMode: 'years',
locale: 'fr',
format: moment()['_locale']['_longDateFormat']['L']
});
});
I dont like that, is there a clean way to get the format?
Upvotes: 11
Views: 21117
Reputation: 2071
Later versions of moment.js have localised formatting available - http://momentjs.com/docs/#/displaying/format/
Localized formats
Because preferred formatting differs based on locale, there are a few tokens that can be used to format a moment based on its locale.
There are upper and lower case variations on the same formats. The lowercase version is intended to be the shortened version of its uppercase counterpart.
Time LT 8:30 PM
Time with seconds LTS 8:30:25 PM
Month numeral, day of month, year L 09/04/1986
l 9/4/1986
Month name, day of month, year LL September 4 1986 ll Sep 4 1986
Month name, day of month, year, time LLL September 4 1986 8:30 PM
lll Sep 4 1986 8:30 PM
Month name, day of month, day of week, year, time LLLL Thursday, September 4 1986 8:30 PM
llll Thu, Sep 4 1986 8:30 PM
L LL LLL LLLL LT are available in version 1.3.0. l ll lll llll are available in 2.0.0. LTS was added in 2.8.4.
So you can get the localised short date format with:
var formattedDate = moment(d).format("l");
Upvotes: 2
Reputation: 123553
You can retrieve locale-specific format strings with the longDateFormat()
of the current localeData()
:
moment.locale('fr');
var localeData = moment.localeData();
var dateFormat = localeData.longDateFormat('LL');
console.log(dateFormat); // D MMMM YYYY
Upvotes: 6