hey guyts,
How to use the latitude and longitude to get local time, not the server time?
because I can't find how to use the latitude and longitude to get local time,
The original method is to use the timezone through google to get the offset for daylight-savings time.
I want to solve this problem.
ESRI does have a World Time Zone layer in the Living Atlas. Query your lat/long point against that service to find out what the time offset is to get your local time.
Steve,
How can I use that for javascript or json?
You would use Query & QueryTask similar to what's shown in this example. The example uses the location where you click on the map but in your case, you would create a point using your lat/long values and then pass that as the value used with query.Geometry.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
<title>Simple Map</title>
<link rel="stylesheet" href="https://js.arcgis.com/3.25/esri/css/esri.css">
<style>
html, body, #map {
height: 100%;
margin: 0;
padding: 0;
}
</style>
<script src="https://js.arcgis.com/3.25/"></script>
<script>
var map;
require(["dojo/on","esri/map","esri/layers/FeatureLayer","esri/tasks/query", "esri/tasks/QueryTask","dojo/domReady!"], function(on,Map,FeatureLayer,Query,QueryTask) {
map = new Map("map", {
basemap: "topo", //For full list of pre-defined basemaps, navigate to http://arcg.is/1JVo6Wd
center: [-100.45, 37.75], // longitude, latitude
zoom: 4
});
var featureLayer = new FeatureLayer("https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/World_Time_Zones/FeatureServer/0",{opacity:0.5});
map.addLayer(featureLayer);
map.on("click", function(evt){
var queryTask = new QueryTask("https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/World_Time_Zones/FeatureServer/0");
var query = new Query();
query.geometry = evt.mapPoint;
query.outFields = ["*"];
queryTask.on("complete", processResults);
queryTask.execute(query);
function processResults(results) {
var features = results.featureSet.features;
if (features.length > 0) {
alert('The zone offset for the clicked location is ' + features[0].attributes.ZONE);
};
};
});
});
</script>
</head>
<body>
<div><h2>Click on the map to get the time zone offset</h2></div>
<div id="map"></div>
</body>
</html>
I tried to update my sample to actually return the local time at the location clicked but I'm having trouble doing so. It's returning the time but off one hour so I don't know if that's due to daylight savings or GMT being 0, etc. Perhaps a more savvy coder can find the error of my ways.
Here's an updated processResults function that returns the timezone offset and the local time (but off by one hour):
function processResults(results) {
var features = results.featureSet.features;
if (features.length > 0) {
var timeOffset = features[0].attributes.ZONE;
var curDate = new Date();
curDate.setUTCMinutes(curDate.getUTCMinutes() + (timeOffset*60));
var hours = curDate.getUTCHours();
var minutes = curDate.getUTCMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
alert('The zone offset for the clicked location is ' + features[0].attributes.ZONE + '\n and the local time is ' + strTime); //curDate.toUTCString());
};
};