如何嘲笑浏览器的时区? [英] How to mock the browser's timezone?

查看:112
本文介绍了如何嘲笑浏览器的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在一个网站来测试位置功能,使这个测试我需要尝试不同的时区。我得到了JavaScript的code中的时区,调用下面的函数:

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();

现在因为我在阿根廷这个函数返回给我180,我需要用不同的时区进行测试。有人知道如何做到这一点?
非常感谢!

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

推荐答案

您可以使用函数这一点。

You could use a function for this.

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

  return new Date().getTimezoneOffset();
}

其中, DEBUG 是一个变量设置早些时候,以确定是否正在测试或没有。

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

然后使用该函数在整个code,而不是Date对象的方法。

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

这篇关于如何嘲笑浏览器的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆