How do I overwride "getTime()" in node.js?
Problem
In my app.js (my main file), I want to override getTime();
So now, when I call created_at.getTime()
in all of my node.js failes that stem from app.js via requires, they should use this new getTime() function that I just overrided.
Basically, I want to change the entire getTime() function to my own, throughout my entire project.
Solution
You can override Date.prototype.getTime
but DO NOT DO THAT as it will break many libraries you would ever use.
You can use the same trick as with JSON.parse
:
var serialized = JSON.stringify(obj, function (key, value) {
if (value instanceof Date) {
return dateHandler(value);
} else {
return value;
}
});
You would define the dateHandler
function.
To keep others in the loop, I'm referring to OP's previous question.
Discussion
View additional discussion.