How to set browsers Timezone in a Web Application ?

1. Create a javascript function called getTimezoneName()

function getTimezoneName() 
{
    tmSummer = new Date(Date.UTC(2005, 6, 30, 0, 0, 0, 0));
    sumTime = -1 * tmSummer.getTimezoneOffset();

    tmWinter = new Date(Date.UTC(2005, 12, 30, 0, 0, 0, 0));
    winTime = -1 * tmWinter.getTimezoneOffset();
    //alert("Summer :"+sumTime+" Winter :"+winTime);        
    if(sumTime == -720 && winTime == -720) return 'Dateline Standard Time';
    if(sumTime == -660 && winTime == -660) return 'Samoa Standard Time';
    if(sumTime == -600 && winTime == -600) return 'Hawaiian Standard Time';
    if(sumTime == -480 && winTime == -480) return 'Alaskan Standard Time';
    if(sumTime == -420 && winTime == -480) return 'Pacific Standard Time';
    if(sumTime == -420 && winTime == -420) return 'Mountain Standard Time';
    if(sumTime == -360 && winTime == -420) return 'Mexico Standard Time';
    if(sumTime == -360 && winTime == -360) return 'Central America Standard Time';
    if(sumTime == -300 && winTime == -360) return 'Central Standard Time';
    if(sumTime == -300 && winTime == -300) return 'Eastern Standard Time';
    if(sumTime == -240 && winTime == -300) return 'Eastern Standard Time';
    if(sumTime == -180 && winTime == -240) return 'Atlantic Standard Time';
    if(sumTime == -240 && winTime == -240) return 'SA Western Standard Time';
    if(sumTime == -240 && winTime == -180) return 'Pacific SA Daylight Time';
    if(sumTime == -150 && winTime == -210) return 'New Foundland Standard Time';
    if(sumTime == -180 && winTime == -120) return 'E. South America Standard Time';
    if(sumTime == -180 && winTime == -180) return 'SA Eastern Standard Time';
    if(sumTime == -120 && winTime == -180) return 'Greenland Standard Time';
    if(sumTime == -60 && winTime == -120) return 'Mid-Atlantic Standard Time';
    if(sumTime == 0 && winTime == -60) return 'Azores Standard Time';
    if(sumTime == -60 && winTime == -60) return 'Cape Verde Standard Time';
    if(sumTime == 0 && winTime == 0) return 'Greenwich Standard Time';
    if(sumTime == 60 && winTime == 0) return 'GMT Standard Time';
    if(sumTime == 120 && winTime == 60) return 'Europe Standard Time';
    if(sumTime == 60 && winTime == 60) return 'W. Central Africa Standard Time';
    if(sumTime == 180 && winTime == 120) return 'Egypt Standard Time';
    if(sumTime == 120 && winTime == 120) return 'South Africa Standard Time';
    if(sumTime == 240 && winTime == 180) return 'Russian Standard Time';
    if(sumTime == 180 && winTime == 180) return 'E. Africa Standard Time';
    if(sumTime == 270 && winTime == 210) return 'Iran Standard Time';
    if(sumTime == 240 && winTime == 240) return 'Arabian Standard Time';
    if(sumTime == 300 && winTime == 240) return 'Caucasus Standard Time';
    if(sumTime == 270 && winTime == 270) return 'Afghanistan Standard Time';
    if(sumTime == 360 && winTime == 300) return 'Ekaterinburg Standard Time';
    if(sumTime == 300 && winTime == 300) return 'West Asia Standard Time';
    if(sumTime == 330 && winTime == 330) return 'India Standard Time';
    if(sumTime == 345 && winTime == 345) return 'Nepal Standard Time';
    if(sumTime == 420 && winTime == 360) return 'N. Central Asia Standard Time';
    if(sumTime == 360 && winTime == 360) return 'Sri Lanka Standard Time';
    if(sumTime == 390 && winTime == 390) return 'Myanmar Standard Time';
    if(sumTime == 420 && winTime == 420) return 'SE Asia Standard Time';
    if(sumTime == 480 && winTime == 420) return 'North Asia Standard Time';
    if(sumTime == 480 && winTime == 480) return 'China Standard Time';
    if(sumTime == 540 && winTime == 480) return 'North Asia East Standard Time';
    if(sumTime == 540 && winTime == 540) return 'Tokyo Standard Time';
    if(sumTime == 600 && winTime == 540) return 'Yakutsk Standard Time';
    if(sumTime == 570 && winTime == 630) return 'Cen. Australia Daylight Time';
    if(sumTime == 570 && winTime == 570) return 'AUS Central Standard Time';
    if(sumTime == 600 && winTime == 600) return 'E. Australia Standard Time';
    if(sumTime == 600 && winTime == 660) return 'AUS Eastern Standard Time';
    if(sumTime == 660 && winTime == 600) return 'Vladivostok Standard Time';
    if(sumTime == 660 && winTime == 660) return 'Central Pacific Standard Time';
    if(sumTime == 720 && winTime == 780) return 'New Zealand Daylight Time';
    if(sumTime == 720 && winTime == 720) return 'Fiji Standard Time';
    if(sumTime == 780 && winTime == 780) return 'Tonga Standard Time';
}

2. Create a lookuptable with all the Timezone return values as above.

CREATE TABLE [dbo].[lookuptable](
    [idLookup] [bigint] IDENTITY(1,1) NOT NULL,
    [groupName] [varchar](20) NOT NULL,
    [lookupName] [varchar](100) NOT NULL,
 CONSTRAINT [PK_lookuptable] PRIMARY KEY (idLookup)
)
GO

INSERT INTO lookupTable (groupName, lookupName) VALUES ('Timezone', 'Dateline Standard Time')
INSERT INTO lookupTable (groupName, lookupName) VALUES ('Timezone', 'Samoa Standard Time')
INSERT INTO lookupTable (groupName, lookupName) VALUES ('Timezone', 'Hawaiian Standard Time')
INSERT INTO lookupTable (groupName, lookupName) VALUES ('Timezone', 'Alaskan Standard Time')
INSERT INTO lookupTable (groupName, lookupName) VALUES ('Timezone', 'Pacific Standard Time')

3. From action/servlet return those values from step 2 to the jsp

4. Create another JavaScript function SetTimeZoneValue()

function setTimeZoneValue()
{
    var currTimeZone = getTimezoneName();
    document.getElementById('timezone_id').value=currTimeZone;
}

 

5. In JSP call setDefaultTimezone() from body onload

<body onload="SetTimezoneName();">

 

Deploy and test the application. Change the timezone from windows date and time properties to see the effect.

Hope the solution helps all 🙂

Advertisements