moment.js:格式化日期以保持区域设置's的日期、月份和年份顺序

moment.js: format a date keeping the locale's day, month and year order

本文关键字:日期 顺序 格式化 js 设置 区域 moment      更新时间:2023-09-26

我正在使用Moment.js,使用本地语言环境功能,以"特定于国家"的顺序格式化一些日期。因此,例如,此代码将打印以荷兰默认值订购的日期:

moment.locale('nl');
nlDate = moment();
nlDate.format('ll'); // "12 nov. 2014"

不同的区域设置将打印不同的顺序:

moment.locale('en');
nlDate = moment();
nlDate.format('ll'); // "Nov 12, 2014"

我想做的是更改输出字符串的格式,保持区域设置的顺序;例如:

(nl) 12 nov. 2014   -->   12-11-'14
(en) Nov 12, 2014   -->   11-12-'14

遗憾的是,根据自定义格式,我无法保持区域设置顺序:

nlDate = moment();
nlDate.locale('nl');
nlDate.format("DD-MM-'YY"); // "12-11-'14"
nlDate.locale('en');
nlDate.format("DD-MM-'YY"); // "12-11-'14"

从上面我想得到:

(nl) 11-12-'14
(en) 12-11-'14

有什么帮助吗?

我在这里和这里阐述我的努力,但不确定我的方向是否正确。

谢谢,Luca

试试这个,

moment.locale("nl").format('L');

moment.locale("en").format('L');

自定义语言设置似乎效果不错:

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

到目前为止,这是我发现的最好的,我对此很满意。