This question already has an answer here:
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!!
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 theDate
prototype, so that all instances ofDate
have the overridden method.You could use a function for this.
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.