How do can I add a new format option to Moment.js locales?

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