Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How do I overwride "getTime()" in node.js?

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.

Problem courtesy of: TIMEX

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.

Solution courtesy of: J. K.

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How do I overwride "getTime()" in node.js?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×