问题
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