2

Possible Duplicate:
How to get the current time in any location

I've heard of open Google WebServices i.e. for maps, and i wondered is there is anything similar to this for time, currency and other functions of Google. Regarding Time i found a similar question, but i want some solution for Commercial use: How to get the current time in any location Oh, i want to call it by AJAX (Client Side).

The Idea is to offer a Dropdown-List with some Cities all around the world, once a City is selected the local time in the City should be shown. I had Problems finding an Algorithm to include all the various Summertime-Changes, so this Approach seems easier.

Community
  • 1
  • 1
efkah
  • 1,628
  • 16
  • 30

1 Answers1

4

Working Demo

Using the HTML5 geolocation API, you can get the current lat/long coords:

var currentPosition;
navigator.geolocation.getCurrentPosition(function(position) {
    currentPosition = position;
});

You can use this to make an ajax request to the web service you mention in your post:

$(function(){
    var tzObject;
    $("button").click(function(){
        console.log(currentPosition);
        var dt = "lat="+currentPosition.coords.latitude +
                 "&lng="+currentPosition.coords.longitude +
                 "&username=demo"; //change to your uName

        $.get("http://api.geonames.org/timezoneJSON", dt, function(msg) {
            tzObject = msg;
            alert(tzObject.time + " " + tzObject.timezoneId);
        });
    });        
});

This returns a JSON object containing the relevant timezone information. Of course, if you have a set list of cities, it's easy to store their lat/long coords and just use those for the request instead. You will need to get a username to use that web service for multiple requests in succession (using 'demo', there's an hourly limit).

nbrooks
  • 18,126
  • 5
  • 54
  • 66
  • thanks for the Response, pretty much exactly what i looked for. i just was hoping that there would be a solution by Google. – efkah Oct 10 '12 at 11:54