I am developing a browser application that is sensitive to the current date.
Throughout the application code, I call new Date and perform calculations based on the current time and accordingly render the view.
To test my application for different potential calendar days, I would have to constantly change the system clock for the past or the future, which is annoying and probably not suitable for my computer.
Therefore, for testing purposes (I would never use this code in the production process), I decided to override the built-in Date constructor by running this in the console:
// create a date object for this Friday: var d = new Date(2012, 0, 20) //override Date constructor so all newly constructed dates return this Friday Date = function(){return d}
Given this assumption, I tried this and got weird results:
var now = new Date Sat Apr 07 2012 00:00:00 GMT-0400 (EDT) now = new Date Tue Jul 10 2012 00:00:00 GMT-0400 (EDT) now = new Date Wed Jul 09 2014 00:00:00 GMT-0400 (EDT) now = new Date Wed Jun 07 2023 00:00:00 GMT-0400 (EDT)
... etc....
My question is: what exactly is going on here?
If I override the constructor to return a static date, why does it give unrelated and ever-increasing dates?
Also, is there an efficient way to override the Date constructor to return a static date in the future, without having to use all the calls to create date instances in my code and modify the output?
Thanks in advance.
EDIT:
I tried my code in a new window and worked as expected.
It seems the culprit was the jQuery UI datepicker plugin, which called its "update" method. When I turn off its call, overriding the date works fine, but as soon as I use datepicker, the weird behavior above happens.
I donβt know why this popular plugin will somehow affect something global. If anyone has any ideas, let me know.
Sorry I didnβt figure out the true criminal before.