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?
Moment's format() method is what you use to convert a Moment object to a string. For example, here's how you would convert a YYYY-MM-DD string into a more human-readable format: const moment = require('moment'); const d = new Date('2019/06/01'); moment(d).format('MMMM d, YYYY'); // June 1, 2019.
Moment construction falls back to js Date. This is discouraged and will be removed in an upcoming major release. This deprecation warning is thrown when no known format is found for a date passed into the string constructor.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With