admin管理员组

文章数量:1277305

I want to be able to format a date as the day and the shortened month. (ex: 20 Sep for en-gb, Sep 20 for en). However, the closest localized format that Moment.js has us the format 'll' which is Sep 20, 2017 for en. I want to be able to create a new format that excludes the year from 'll'. How can I best achieve this?

The end result I want (assuming today is Sept 20):

moment.locale('en');
moment().format('my-new-format')  ---> Sep 20
moment.locale('en-gb');
moment().format('my-new-format')  ---> 20 Sep
moment().format('ll')             ---> 20 Sep, 2017

I want to be able to format a date as the day and the shortened month. (ex: 20 Sep for en-gb, Sep 20 for en). However, the closest localized format that Moment.js has us the format 'll' which is Sep 20, 2017 for en. I want to be able to create a new format that excludes the year from 'll'. How can I best achieve this?

The end result I want (assuming today is Sept 20):

moment.locale('en');
moment().format('my-new-format')  ---> Sep 20
moment.locale('en-gb');
moment().format('my-new-format')  ---> 20 Sep
moment().format('ll')             ---> 20 Sep, 2017
Share Improve this question edited Sep 20, 2017 at 1:53 breakfast asked Sep 20, 2017 at 1:35 breakfastbreakfast 831 silver badge6 bronze badges 3
  • 1 Hope this helps (similar question) - stackoverflow./questions/39028993/… – Phani Kumar M Commented Sep 20, 2017 at 2:05
  • That's definitely a similar question! However, the answer isn't too helpful (aside from providing more evidence that maybe how I want to solve this problem isn't possible). I don't want to directly edit the libary or locale files. – breakfast Commented Sep 20, 2017 at 2:16
  • Does this answer your question? custom long date format in moment js – Mogsdad Commented Dec 30, 2019 at 21:16
Add a ment  | 

2 Answers 2

Reset to default 8

That seems to work with me... :

moment.updateLocale("en", { longDateFormat : { "[my-new-format]" : "MMM D" } });
moment.updateLocale("en-gb", { longDateFormat : { "[my-new-format]" : "DD MMM" } });

And then

moment.locale('en');
moment().format('[my-new-format]');

So you don't overwrite existing formats.

You can overwrite the localized by using the built in updateLocale

moment.updateLocale('en', {
    longDateFormat : {
        LT: "h:mm A",
        LTS: "h:mm:ss A",
        L: "MM/DD/YYYY",
        l: "M/D/YYYY",
        LL: "MMMM Do YYYY",
        ll: "MMM D YYYY",
        LLL: "MMMM Do YYYY LT",
        lll: "MMM D YYYY LT",
        LLLL: "dddd, MMMM Do YYYY LT",
        llll: "ddd, MMM D YYYY LT"
    }
});

If you don't want to override the already existing locale, but still use it, I would go with a dictionary like this since adding new formats is not supported to my knowledge.

const formats = {
  en: "MMM D",
  sv: "DD MMM"
}
const customFormat() => formats[moment.locale()];
moment.locale("en");
console.log(moment().format(customFormat()));  // Jan 1
moment.locale("sv");
console.log(moment().format(customFormat())); // 01 Jan

本文标签: javascriptHow do can I add a new format option to Momentjs localesStack Overflow