McSas McSas - 11 months ago 53
Javascript Question

How to mock the browser's timezone?

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

Answer Source

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.