Search code examples
javascriptjquerydatejs

How to cache the JavaScript Date object?


I am using Date.js to support many cultures in my web app. The problem here is that date.js has code like this.

  Date.prototype._toString = Date.prototype.toString;
  Date.prototype.toString = function () {
    //doing something
        return this._toString();
   }

when I use another culture file, it also contains this definition. so logically my doc has this

//date.js
     Date.prototype._toString = Date.prototype.toString;
     Date.prototype.toString = function () {
     //doing something
         return this._toString();
     }
     //date-fr-FR.js
     Date.prototype._toString = Date.prototype.toString;
     Date.prototype.toString = function () {
         //doing something
         return this._toString();
     }

I am refering to both date.js and date-fr-FR.js in my web app.

The problem is when I use the toString function var d = new Date().toString(); it throws an Out of stack space msg, because of the recursive calls. Is there any way to cache the orginal Date object and rest it back, because i don't want to remove date.js from doc


Solution

  • Instead of including both date.js and date-fr-FR.js, you only need to include the fr-FR.js file to change the culture, which you will find in the src/globalization folder in the Datejs-all-Alpha1.zip file. The fr-FR.js file only contains the culture specific data, and it should override what is already included in date.js, without redefining the functionality.