In a React project, I'm using moment.js (version 2.22.2) and would like to extend it and add new functions to it in order to simplify common use cases.
I've tried several different ways of extending moment.js, but after lots of experimentation and Googling, I haven't been able to figure it out.
None of the following work:
#1:
import moment from 'moment';
moment.formatForDb = () => {
return this.format('YYYY-MM-DD');
};
export default moment;
#2:
import moment from 'moment';
moment.prototype.formatForDb = () => {
return this.format('YYYY-MM-DD');
};
export default moment;
#3:
import moment from 'moment';
moment.fn.formatForDb = () => {
return this.format('YYYY-MM-DD');
};
export default moment;
Does anyone know how to extend moment.js so that new functions can be added to it? Thank you.