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
2 Answers
Reset to default 8That 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
版权声明:本文标题:javascript - How do can I add a new format option to Moment.js locales? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741291481a2370574.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论