Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get clients Time zone in JavaScript?

who can write a function to get clients Time zone,return value like:EDT EST IST and so on

like image 459
artwl Avatar asked Apr 24 '12 13:04

artwl


3 Answers

toTimeString() method give time with the timezone name try out below...

var d=new Date();
var n=d.toTimeString();     

ouput

03:41:07 GMT+0800 (PHT) or 09:43:01 EDT 

Demo

or

Check : Automatic Timezone Detection Using JavaScript

download jstz.min.js and add a function to your html page

    <script language="javascript">
        function getTimezoneName() {
            timezone = jstz.determine_timezone()
            return timezone.name();
        }
    </script>
like image 78
Pranay Rana Avatar answered Oct 23 '22 23:10

Pranay Rana


Use the Date().getTimezoneOffset() function and then build a hash table from this URL timeanddate to relate it to if you want to use the time zone value.

like image 38
vipergtsrz Avatar answered Oct 23 '22 23:10

vipergtsrz


If you look at the result of calling the toString method of a Date object, you'll get a value that's something like "Tue Apr 24 2012 23:30:54 GMT+1000 (AUS Eastern Standard Time)". This will depend on what your system locale is set to.

From there you can match each capital letter within the parentheses.

var paren = new Date().toString().match(/\(.+\)/);
return paren ? paren[0].match(/([A-Z])/g).join("") : "";

The catch is that not every browser will include the parenthesised values.

If you're targeting Firefox with a known Java plugin, you can also exploit the java object in Javascript to create a new TimeZone (java.util.TimeZone) object based on a name (eg. "America/Los_Angeles"), then call the getDisplayName method to give you the name.

like image 44
daniel Avatar answered Oct 24 '22 00:10

daniel