16

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!!

McSas
  • 2,224
  • 3
  • 27
  • 42

2 Answers2

37

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
JC Brand
  • 2,652
  • 18
  • 18
  • 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](https://en.wikipedia.org/wiki/Inversion_of_control). Would be better if there's a [proper developer tool](http://stackoverflow.com/questions/4976696/how-do-i-test-my-browser-timezone-dependent-application-againts-different-timezo#comment58949204_4976769) for it.... – Pacerier Apr 09 '16 at 14:22
  • 1
    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;` – mikeapr4 Aug 22 '16 at 13:04
  • Thanks @mikeapr4, you're right. I added the missing assignment. – JC Brand Aug 22 '16 at 14:13
  • 4
    If you're using moment.js this didn't work for me. Instead I got the original timezone using https://momentjs.com/timezone/docs/#/using-timezones/guessing-user-timezone/ and then set it using https://momentjs.com/timezone/docs/#/using-timezones/default-timezone/ and also used that to set it back at the end of the test. – Liron Yahdav Mar 16 '17 at 22:43
  • 1
    this unfortunately didn't work with jest and react-testing-library. I did that in beforeEach and in actual test I do `new Date().getTimezoneOffset()` and it is still returning my current timezone offset instead of the one I specified in beforeEach. – Alena Levina May 16 '23 at 16:01
-8

You could use a function for this.

function getTimezoneOffset() {
  if (DEBUG) {
    return 600; // for Australian Eastern Standard Time
  }

  return new Date().getTimezoneOffset();
}

Where DEBUG is a variable set earlier on to determine whether you're testing or not.

Then use that function throughout your code, instead of the method on the Date object.

Brendan
  • 3,415
  • 24
  • 26
  • 6
    This doesn't help if you're using a library like `MomentJS` -- you can't force it to use your wrapper function. – Carl Meyer Mar 30 '15 at 19:50