我需要根据用户的 IP 或 http header 了解我的用户当前所在的时区。
关于这个问题,我得到了很多答案,但我无法理解那些答案。有人说使用 -new Date().getTimezoneOffset()/60
( from here )。但是这是什么意思?
我的 (index.php) 页面的根目录中有一个 date_default_timezone_set("Asia/Calcutta");
。因此,为此我必须动态获取时区并将其设置为 Asia/Calcutta
。
最佳答案
用代码总结 Matt Johnson 的回答:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js">
</script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
var tz = jstz.determine(); // Determines the time zone of the browser client
var timezone = tz.name(); //For e.g.:"Asia/Kolkata" for the Indian Time.
$.post("url-to-function-that-handles-time-zone", {tz: timezone}, function(data) {
//Preocess the timezone in the controller function and get
//the confirmation value here. On success, refresh the page.
});
});
</script>
关于javascript - 如何检测用户的时区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16525617/