How to mock the browser's timezone?

25,411

The accepted answer doesn't really mock the Date.getTimezoneOffset method, instead it expects you to use a different method with the same name.

It won't work on Date objects themselves and as Carl Meyer points out, it won't work for libraries like MomentJS.

A better way is to override the getTimezoneOffset method on the Date prototype, so that all instances of Date have the overridden method.

d = new Date(); // Mon Jul 13 2015 10:58:12 GMT+0200 (CEST)
alert(d.getTimezoneOffset()); // -120, My local "real" timezone.

// Save the original method.
var getTimezoneOffset = Date.prototype.getTimezoneOffset;

Date.prototype.getTimezoneOffset = function () {
    return 160;
}
// Now Date objects will have the mocked timezone offset
alert(d.getTimezoneOffset()); // 160, The mocked timezone.

// Now restore the method to its original version
Date.prototype.getTimezoneOffset = getTimezoneOffset;
alert(d.getTimezoneOffset()); // -120
Share:
25,411

Related videos on Youtube

McSas
Author by

McSas

I am a Software Engineer from Argentina, I am here to learn and try to help the community =)

Updated on August 23, 2020

Comments

  • McSas
    McSas over 3 years

    I want to test a location feature in a web site, to make this test I need to try different time-zones. I obtain the timezone with a javascript code, calling the following function:

    var offset = new Date().getTimezoneOffset();
    

    Now this function returns to me 180 because I am in Argentina, I need to test with different time-zones. Somebody knows how to do this? Many thanks!!

    • Heretic Monkey
      Heretic Monkey over 11 years
      Change the time-zone on your computer. That's where the browser gets it.
    • Miguel Madero
      Miguel Madero about 9 years
      You could also start your browser with a different TZ: stackoverflow.com/questions/11453740/…
  • Carl Meyer
    Carl Meyer about 9 years
    This doesn't help if you're using a library like MomentJS -- you can't force it to use your wrapper function.
  • Pacerier
    Pacerier about 8 years
    Depending on which library you use, some libraries would want to do it "their way". And trying to "fix" these libraries with additional code might break things unexpectedly.... especially for those using hollywood principle. Would be better if there's a proper developer tool for it....
  • mikeapr4
    mikeapr4 over 7 years
    The answer makes sense to me, but looking at the code, it might be missing the statement to save the original version, something like var getTimezoneOffset = Date.prototype.getTimezoneOffset;
  • JC Brand
    JC Brand over 7 years
    Thanks @mikeapr4, you're right. I added the missing assignment.
  • Rob
    Rob about 7 years
    If you're using moment.js this didn't work for me. Instead I got the original timezone using momentjs.com/timezone/docs/#/using-timezones/… and then set it using momentjs.com/timezone/docs/#/using-timezones/default-timezon‌​e and also used that to set it back at the end of the test.