Create a gist now

Instantly share code, notes, and snippets.

UTC to current time zone converter
/*
* Dojo AMD helper function to convert dates stored as UTC milliseconds from January 1, 1970 to a date/time format more fitting
* for current location.
* Reason: ArcGIS Server stores dates without times as midnight UTC. When those times are translated into real dates,
* the browser often corrects the date for time zone differences, moving the date back several hours in the U.S.
*/
/* globals define */
define(function () {
var oneDay = 24 * 60 * 60 * 1000;
/**
* function to correct date values created when data source stores time in UTC.
* @param {number | Date} value - either milliseconds from January 1, 1970, at 12:00:00 AM UTC, or Date object created by that criteria
* @returns {Date} - date object corrected for current time zone, to reverse the natural correction browsers make to dates.
*/
return function (value) {
var val;
if (value === undefined || value === null) {
return value;
}
val = typeof value.getTime === "function" ? value.getTime() : value;
if (val % oneDay === 0) {
return val + (new Date(val).getTimezoneOffset() * 60 * 1000);
}
return val;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment