javascript moment.js:如何获得短日期格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34213343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
moment.js: how to get short date format?
提问by Olivier Pons
My application sends an HTML file with javascript like this:
我的应用程序发送一个带有 javascript 的 HTML 文件,如下所示:
$(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
?
随着时间的推移,当我设置为语言环境时,有没有办法获取配置的短日期格式,例如“ 'j F Y'
” 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?
我不喜欢那样,有没有一种干净的方法来获取格式?
采纳答案by Jonathan Lonowski
You can retrieve locale-specific format strings with the longDateFormat()
of the current localeData()
:
您可以检索与特定的语言环境,格式字符串longDateFormat()
的电流localeData()
:
moment.locale('fr');
var localeData = moment.localeData();
var dateFormat = localeData.longDateFormat('LL');
console.log(dateFormat); // D MMMM YYYY
回答by Steve Owen
Later versions of moment.js have localised formatting available - http://momentjs.com/docs/#/displaying/format/
更高版本的 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.
本地化格式
由于首选格式因区域设置而异,因此有一些标记可用于根据其区域设置格式化片刻。
相同格式有大写和小写变体。小写版本旨在成为其大写对应版本的缩短版本。
时间 LT 晚上 8:30
带秒的时间 LTS 8:30:25 PM
月份数字、月份中的日期、年份 L 09/04/1986
l 9/4/1986
月份名称、月份中的日期、年份 LL 1986 年 9 月 4 日 ll 1986 年 9 月 4 日
月份名称、月份的日期、年份、时间 LLL 1986 年 9 月 4 日晚上 8:30
1986 年 9 月 4 日晚上 8:30
月份名称、月份中的某一天、星期几、年份、时间 LLLL 1986 年 9 月 4 日星期四晚上 8:30
1986 年 9 月 4 日星期四晚上 8:30
L LL LLL LLLL LT 在 1.3.0 版中可用。l ll lll llll 在 2.0.0 中可用。LTS 是在 2.8.4 中添加的。
So you can get the localised short date format with:
因此,您可以获得本地化的短日期格式:
var formattedDate = moment(d).format("l");