batista champ batista champ - 4 months ago 10
Javascript Question

How to identify time zone of client machine using JavaScript?

I need to capture local time zone of client based on the browser

Like if client comes from US: Time1 and

if client comes from India: Time2

JavaScript method to get datetime based on browser

save value in hidden field

call or execute above JavaScript function on server side

get back value from hidden field to server side.

Answer
<script>    
                var offset = (new Date()).getTimezoneOffset();
                var timezones = {
                    '-12': 'Pacific/Kwajalein',
                    '-11': 'Pacific/Samoa',
                    '-10': 'Pacific/Honolulu',
                    '-9': 'America/Juneau',
                    '-8': 'America/Los_Angeles',
                    '-7': 'America/Denver',
                    '-6': 'America/Mexico_City',
                    '-5': 'America/New_York',
                    '-4': 'America/Caracas',
                    '-3.5': 'America/St_Johns',
                    '-3': 'America/Argentina/Buenos_Aires',
                    '-2': 'Atlantic/Azores',
                    '-1': 'Atlantic/Azores',
                    '0': 'Europe/London',
                    '1': 'Europe/Paris',
                    '2': 'Europe/Helsinki',
                    '3': 'Europe/Moscow',
                    '3.5': 'Asia/Tehran',
                    '4': 'Asia/Baku',
                    '4.5': 'Asia/Kabul',
                    '5': 'Asia/Karachi',
                    '5.5': 'Asia/Calcutta',
                    '6': 'Asia/Colombo',
                    '7': 'Asia/Bangkok',
                    '8': 'Asia/Singapore',
                    '9': 'Asia/Tokyo',
                    '9.5': 'Australia/Darwin',
                    '10': 'Pacific/Guam',
                    '11': 'Asia/Magadan',
                    '12': 'Asia/Kamchatka'
                };
                var a = timezones[-offset / 60];
                document.cookie = "cookieName=" + a;               
            </script>