May 6, 2015

Get Time Zone and UTC From Latitude and Longitude Using JavaScript

This document will demonstrate you to get the time of a location using Google Time API in java script.Simply we need the Latitude and Longitude of location to do that.Hope following code lines will help you.

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
  
$(document).ready(function(){

//Sending A Default Location
var Latitude  = 6.0535185;
var Longitude = 80.22097729999996;
getTimeUsingLatLng(Latitude,Longitude);

function getTimeUsingLatLng(lat,lng){
  var times_Stamp = (Math.round((new Date().getTime())/1000)).toString();
 $.ajax({
  url:"https://maps.googleapis.com/maps/api/timezone/json?location=" + lat + "," + lng + "&timestamp=" + times_Stamp,
  cache: false,
  type: "POST",
  async: false,
 }).done(function(response){

  if(response.timeZoneId != null){
   var Cur_Date = new Date();
  var UTC = Cur_Date.getTime() + (Cur_Date.getTimezoneOffset() * 60000);
  var Loc_Date = new Date(UTC + (1000*response.rawOffset) + (1000*response.dstOffset));
       $("#timeOfLocation").html("Time Of The Location Is " + Loc_Date.toLocaleString());
     }
   });
  } 
});
</script>
</head>
<body>
  <p id = "timeOfLocation"></p>
</body>
</html>


No comments:

JWT Token Decode Using Jquery

When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...