javascript - Extending MomentJS to override toString() function -
i'm trying extend moment's tostring function return date in different format.
i.e. want create momentdate below:
// return "sat dec 12 2015 00:00:00 gmt+1100" moment('2015-12-12').tostring(); // return custom format, e.g. "2015-12-12" momentdate('2015-12-12').tostring(); i've been trying no success. i'm not sure if it's possible because of how moment written thought ask here.
my question answered here:
https://stackoverflow.com/a/34626333/2248573
note: solution works v2.11.0.
function extendedmoment() { var self = moment(); self.__proto__ = extendedmoment.prototype; return self; } extendedmoment.prototype.__proto__ = moment.prototype; extendedmoment.prototype.tostring = function(){ return this.format('yyyy-mm-dd') } document.write("original: " + moment().tostring()); document.write("<br/>"); document.write("extended: " + extendedmoment().tostring()); <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.0/moment.js"></script>
Comments
Post a Comment