HOW TO show local time for a location based on its latitude and longitude
Here is a GitHub Gist snippet that shows how to get local time for a location based on its latitude and longitude using World Weather Online's Time Zone API & jQuery -
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function time(lat, long) { | |
//replace the value for the key parameter with your own | |
var url = "http://api.worldweatheronline.com/free/v1/tz.ashx?q=" + lat + "," + long + "&format=json&key={key}&callback=tfunc"; | |
//reference to jQuery library is required prior to using this function | |
$.getScript(url, function (response) { }); | |
} | |
window.tfunc = function (response) { | |
var time = ""; | |
time = "Local Time:" + response.data.time_zone[0].localtime; | |
$("#time").append(time); // #time represents a div whose id has the value time | |
}; |
This can be useful in scenarios where you have to display time for a location relative to another.
Comments
Post a Comment