moment.js: 如何获取短日期格式?
moment.js: how to get short date format?
我的应用程序发送一个 HTML 文件 javascript 像这样:
$(function () {
moment.locale('fr');
$('#datetimepicker9').datetimepicker({
viewMode: 'years',
locale: 'fr',
format: '' /* <========= problem! */
});
});
现在,当我设置为语言环境时,有没有办法获取配置的短日期格式,例如 fr
的“'j F Y'
”?
我找到了它,但它是 hack-ish:
moment()['_locale']['_longDateFormat']['L']
所以我现在的代码:
$(function () {
moment.locale('fr');
$('#datetimepicker9').datetimepicker({
viewMode: 'years',
locale: 'fr',
format: moment()['_locale']['_longDateFormat']['L']
});
});
我不喜欢那样,有没有一种干净的方法来获取格式?
您可以使用 the current localeData()
的 longDateFormat()
检索特定于区域设置的格式字符串:
moment.locale('fr');
var localeData = moment.localeData();
var dateFormat = localeData.longDateFormat('LL');
console.log(dateFormat); // D MMMM YYYY
moment.js 的更高版本具有可用的本地化格式 - 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.
因此您可以获得本地化的短日期格式:
var formattedDate = moment(d).format("l");
我的应用程序发送一个 HTML 文件 javascript 像这样:
$(function () {
moment.locale('fr');
$('#datetimepicker9').datetimepicker({
viewMode: 'years',
locale: 'fr',
format: '' /* <========= problem! */
});
});
现在,当我设置为语言环境时,有没有办法获取配置的短日期格式,例如 fr
的“'j F Y'
”?
我找到了它,但它是 hack-ish:
moment()['_locale']['_longDateFormat']['L']
所以我现在的代码:
$(function () {
moment.locale('fr');
$('#datetimepicker9').datetimepicker({
viewMode: 'years',
locale: 'fr',
format: moment()['_locale']['_longDateFormat']['L']
});
});
我不喜欢那样,有没有一种干净的方法来获取格式?
您可以使用 the current localeData()
的 longDateFormat()
检索特定于区域设置的格式字符串:
moment.locale('fr');
var localeData = moment.localeData();
var dateFormat = localeData.longDateFormat('LL');
console.log(dateFormat); // D MMMM YYYY
moment.js 的更高版本具有可用的本地化格式 - 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.
因此您可以获得本地化的短日期格式:
var formattedDate = moment(d).format("l");