skip to Main Content

I’m trying to test an extension designed to work only in my workplace (intranet).
More specifically, I placed breakpoints in points of an alarm-triggered routine, where I read the system time at 2 specific times of the day; one close to midnight and the other around 05:00 P.M.
In order not to have to wait for these two moments of the day, I would like to move the system time close to these two times, but I can’t do it because system administrators have disabled this option.

So I’m here to ask for your help.
It is possible to "override" the Date object so that it returns a fake time shifted (forward or backward) by a constant value decided by me on the spot.

Furthermore, time must flow normally.

Taking a practical example, if I now launch this command in the console:
dt = new Date()

I get

Fri Apr 14 2023 10:50:00 GMT+0200

after overriding it should display

Fri Apr 14 2023 23:50:00 GMT+0200

I want the time moved forward 13 hours.
Of course I should activate this movement with a command like:

shiftDate.move(13)

and to make things right again something like:

shiftDate.reset();

All methodsproperties of the Date object must remain unchanged.

Can you give me a hand please?

Thanks in advance

3

Answers


  1. Chosen as BEST ANSWER

    I found a solution, perhaps trivial but it seems to work.

    const fakeDate = {
        set:        function(y, m, d, h, m, s) {
                        if (typeof DateObjBck !== 'undefined')
                            this.reset();
    
                        /*Saving the original Date object */
                        DateObjBck = Date;
    
                        /* set the clock closed to midnight */
                        var startFrom = new Date(...arguments);
    
                        /*Calculating the constat offset between real time and the fake one */
                        const offsetMs = startFrom.getTime() - (new DateObjBck).getTime();
                        
                        /* Now I rewrite the Date object */
                        Date =  function(...args) {
                                    if (args.length > 0)
                                        return new DateObjBck(...args);
                                    else {
                                        var veryNow = (new DateObjBck).getTime();
                                        return new DateObjBck(veryNow + offsetMs)                                   
                                    }
                                };
                        Date.now =  function() {
                                        var veryNow = (new DateObjBck).getTime();
                                        return veryNow + offsetMs
                                    }
                    },
        reset:  function() {
                        Date = DateObjBck;
                        DateObjBck = undefined
                }
    }
    
    fakeDate.set(2023, 3, 13, 23, 55, 0);
    console.log('0', new Date());
    console.log('1', new Date);
    console.log('2', Date.now());
    console.log('3', new Date(2020, 5, 15, 22, 50, 0));
    console.log('4', new Date);
    fakeDate.reset();
    console.log('5', new Date);


  2. This article might help.
    https://www.browserstack.com/guide/change-time-zone-in-chrome-for-testing

    Try Method 1: Using Developer Tools to Change Chrome Timezone in particular.

    1. Open DevTools in Chrome -> Open the Console drawer.
    2. Click on the three-dotted menu -> Click on More tools -> Sensors.
    3. From the Sensors tab, set the location according to your preference and define the specific timezone.
    Login or Signup to reply.
  3. Because Date is a class, we can extend it to provide additional functionality without tampering with the existing static properties/methods.

    For instance, you can write a new class like ShiftedDate below:

    class ShiftedDate extends Date{
        static #offsetMs = 0; //private static field
        static move(ms){
            this.#offsetMs = ms;
        }
        static moveHours(hours){
            this.move(hours*60*60*1000); 
        }
        static setTime(...args){
            this.move(new Date(...args)-new Date());
        }
        static now(){ //The only static function I can think of that doesn't depend on instance time. 
            return super.now()+this.#offsetMs;
        }
        constructor(...args){
            super(...args); 
            if(args.length===0)this.setTime(this.getTime()+ShiftedDate.#offsetMs); //Don't shift explicit dates
        }
    }
    

    And use it as such:

    const originalDate = new ShiftedDate();
    console.log(new ShiftedDate()); //2023-04-15T11:49:01.943Z
    
    ShiftedDate.moveHours(2);
    console.log(new ShiftedDate()); //2023-04-15T13:49:02.018Z
    
    ShiftedDate.setTime("7/12/2012");
    console.log(new ShiftedDate()); //2012-07-12T00:00:00.000Z
    
    console.log(originalDate); //2023-04-15T11:49:01.943Z - Old dates are not updated
    console.log(ShiftedDate.now()); //1342051200000 (7/12/12)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search