views:

331

answers:

4

Hello,

I want to get clients Time Zone offset from his IP address for my web app. I have tried using Javascripts getTimezone function, but, some clients have their timezone or time set incorrectly. I want to get the offset and render some information back on client based on their timezone.

Thanks for taking time to read the question. I am open for alternate approaches too.

Regards, Vamyip

A: 

I use this:

 // Gets the Browsers timezone offset in minutes
    function getClientTimezoneOffsetInMinutes()
    {
        var rightNow = new Date();
        var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
        var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
        var temp = date1.toGMTString();
        var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
        var temp = date2.toGMTString();
        var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
        var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
        var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60);
        return hoursDiffStdTime;
    }
Mark Redman
A: 

google 'geolocation ip'

There are API's you can call provided by companies. Some are free, some are not.

TheGeekYouNeed
+2  A: 

If you can call a web service, you might like to try ipinfodb.com. For example:

http://ipinfodb.com/ip_query.php?ip=69.59.196.211&timezone=true

returns:

<Response> <Ip>69.59.196.211</Ip> <Status>OK</Status> <CountryCode>US</CountryCode> <CountryName>United States</CountryName> <RegionCode>41</RegionCode> <RegionName>Oregon</RegionName> <City>Corvallis</City> <ZipPostalCode>97333</ZipPostalCode> <Latitude>44.4698</Latitude> <Longitude>-123.343</Longitude> <TimezoneName>America/Los_Angeles</TimezoneName> <Gmtoffset>-25200</Gmtoffset> <Isdst>1</Isdst> </Response>

A faster option is to use the free MaxMind Geolite city. If it is not good enough, you can apparently upgrade to a more accurate paid-version. I can't speak for the quality of the paid version, as I have never used it. You can download the file binary blob version of the same database, and then use the C# class to query it.

fmark
A: 

You may try out the database service provided by IP2Location.com. The Website offers several packages come with detect the IP address and return the time zone data as well as other geolocation info.

JaceyKala