Override the moment js default invalid date text

大兔子大兔子 提交于 2021-02-19 04:54:22

问题


How can I override the moment.js

var defaultInvalidDate = 'Invalid date';

without changing the moment.js file. Just like my site overrides certain bootstrap css styles with a Site.css, so when bootstrap is updated I dont lose the changes, is there any way to make overrides for momentjs?

Thanks in advance


回答1:


Just update your current locale used by moment.js using moment.updateLocale(localeName, config)

moment.updateLocale(moment.locale(), { invalidDate: "Invalid Date Updated" })

Here is the working example:

console.log(moment(new Date("Aa")).format(""));
moment.updateLocale(moment.locale(), { invalidDate: "Invalid Date Updated" })
console.log(moment(new Date("Aa")).format(""));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>



回答2:


You can also edit the locale file in moment package directly.

For an example to change invalid date message for DE, find the locale file which is moment/locale/de.js and then add following under moment.defineLocale:

invalidDate: function() {
  return 'Date Invalid';
}

JS Fiddle https://fiddle.jshell.net/dzcz07um/



来源:https://stackoverflow.com/questions/38953566/override-the-moment-js-default-invalid-date-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!