Alan Ball
Alan Ball

Reputation: 593

Override the moment js default invalid date text

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

Upvotes: 4

Views: 4967

Answers (2)

Thilina Hasantha
Thilina Hasantha

Reputation: 130

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/

Upvotes: 1

Adnan Umer
Adnan Umer

Reputation: 3689

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>

Upvotes: 10

Related Questions