Skip to content

Instantly share code, notes, and snippets.

@raykendo
Created March 30, 2016 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raykendo/847b21ffa08461d5d92349342b7c61fd to your computer and use it in GitHub Desktop.
Save raykendo/847b21ffa08461d5d92349342b7c61fd to your computer and use it in GitHub Desktop.
Formatting Dates Only in ArcGIS Online web map popups.
require(["esri/arcgis/utils"], function (arcgisUtils) {
//...
// functionality to fix dates
var oneDay = 24 * 60 * 60 * 1000;
function timeFix(value) {
var dateShift, val;
if (value === undefined || value === null) {
return value;
}
val = value.getTime === "function" ? value.getTime() : value;
// assumption: we want to shift datetimes where the time is midnight UTC (down to the millisecond);
if (val % oneDay === 0) {
// if the date has a time of midnight UTC, we shift it by the timezone offset
dateShift = new Date(val).getTimezoneOffset() * 60 * 1000;
return val + dateShift;
}
}
// create the map.
arcgisUtils.createMap("/* insert web map id here */", "mapdiv", {}).then(function (response) {
var map = response.map;
// do stuff
if (map.infoWindow) {
map.infoWindow.on("set-features", function () {
// for each selected feature in the infoWindow popup...
map.infoWindow.features.forEach(function (feature) {
// get the layer from the feature
var layer = feature._graphicsLayer || feature._layer || null;
// look at the fields in the layer (if present).
if (layer && layer.fields && layer.fields.length) {
layer.fields.forEach(function(field) {
// if the field type is a date field...
if (field.type === "esriFieldTypeDate") {
// fix the time.
feature.attributes[field.name] = timeFix(feature.attributes[field.name]);
}
});
}
});
})
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment