2009/05/23

Javascript: Get client's TimeZone

How to know client's time and time zone? The simplest way that can do this is using the javascript.
<script type="text/javascript">
function displayTime()
{
var localTime = new Date();
//this one will give you the GMT offset
var timezone = localTime.getTimezoneOffset()/60 * (-1);
var div = document.getElementById("div1");
div.innerText = "GMT" + timezone;
}
</script>
Or simply use
<script type="text/javascript">
function displayTime()
{
var div = document.getElementById("div1");
div.innerText = (new Date()).toString();
}
</script>

2 comments:

  1. what about DST ?

    ReplyDelete
  2. As the above mentions, this won't work because of DST.

    ReplyDelete