Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7093209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:24:38+00:00 2026-05-28T08:24:38+00:00

I am developing a browser application that is sensitive to the current date. Throughout

  • 0

I am developing a browser application that is sensitive to the current date.

Throughout my application’s code, I call new Date and perform calculations based on the current time and render the view accordingly.

In order to test my application for different potential calendar days, I would have to constantly change my system clock to the past or future, which is an annoyance and probably not healthy for my computer.

So purely for testing purposes (I would never use this code in production), I decided to override the built-in Date constructor by doing 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}

With this assumption in mind, I tried this and got strange 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)

…and so on….

My question is, what exactly is going on here?

If I overrode the constructor to return a static date, why does it give unrelated and constantly incrementing dates?

Also, is there an effective way I can override the Date constructor to return a static date in the future without having to go through all date instantiation calls in my code and modifying the output?

Thanks in advance.

EDIT:

I tried my code in a fresh window and it worked as expected.

It seems the culprit was the jQuery UI datepicker plugin which was calling its “refresh” method. When I disable its call, the date overriding works normally, but as soon as I use the datepicker, the strange behavior above occurs.

No idea why this popular plugin would somehow affect something global like this. If anyone has any ideas, let me know.

Sorry for not figuring out the true culprit earlier.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:24:38+00:00Added an answer on May 28, 2026 at 8:24 am

    I tested your code:

    // 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;};
    
    var now = new Date()
    console.log(now);
    
    now = new Date()
    console.log(now);
    
    now = new Date()
    console.log(now);
    
    now = new Date()
    console.log(now);
    

    And the result ???? Why so different?

    Date {Fri Jan 20 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}
    Date {Fri Jan 20 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}
    Date {Fri Jan 20 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}
    Date {Fri Jan 20 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}
    

    EDIT:

    I saw that whenever you interact with the Date Picker, the behavior goes different. Try another test, change the now is something like interact with Date Picker:

    // 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;};
    
    var now = new Date();
    var another = new Date();
    console.log(now);
    
    another.setDate(13);
    
    now = new Date()
    console.log(now);
    

    And the result is:

    Date {Fri Jan 20 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}
    Date {Fri Jan 13 2012 00:00:00 GMT+0700 (SE Asia Standard Time)}
    

    So, what goes wrong?
    You already overridden core Date function by

    Date = function(){return d;}; // after construction, all date will be d (2012-01-20)
    var now = new Date(); // you instantiate a date, but actually now variable is d (2012-01-20)
    var another = new Date(); // you instantiate a date, but another is still d (2012-01-20)
    another.setDate(13); // change another date to 13 is to change now to 13 (because now and another is still one d)
    
    now = new Date() // still d
    console.log(now); // print out now (2012-01-13)
    

    So, you overrides core Date function by a function that causes all date use the same (just one) instance, which is d (2012-01-20). Change any dates affect others.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an application that uses javascript to perform some arithmetical calculations in
I am interested in developing a workstation-based application that communicates with a proprietary data
I'm developing a kind of web-based intranet application that runs on wamp, other systems/hosts
I am developing an application for Mac OS X (I am new to that
Developing websites are time-consuming. To improve productivity, I would code a prototype to show
Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux.
I'm developing an application that reads data from a SQL server database (migrated from
We have plans of developing a web application that will have to communicate with
I am developing a Java web services application that is (mostly) to be used
I am developing Sencha touch application that will work on iPhone. Now application is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.