Skip to content

Instantly share code, notes, and snippets.

@raykendo
Last active March 29, 2016 15:48
Show Gist options
  • Save raykendo/19eb8873997ef31985d4 to your computer and use it in GitHub Desktop.
Save raykendo/19eb8873997ef31985d4 to your computer and use it in GitHub Desktop.
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