Skip to content

Instantly share code, notes, and snippets.

@kmaida
Created February 26, 2014 16:10
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 kmaida/9232637 to your computer and use it in GitHub Desktop.
Save kmaida/9232637 to your computer and use it in GitHub Desktop.
This function finds the user's local date/time converted from EDT at a set time in the future. Useful for expiring cookies on a regular basis. By default, expires cookie once-per-day at 12:01 AM EDT the next day, every day. Modify offsets accordingly.
function findLocalExpTime() {
var curTime = new Date(),
expTime = new Date(),
baseZoneOffset = 4, // Base timezone offset from UTC (4 in this example for EDT, change as appropriate)
tmpYear = curTime.getUTCFullYear(),
tmpMonth = curTime.getUTCMonth(),
tmpDate = curTime.getUTCDate(),
tmpHrs = curTime.getUTCHours(),
tmpMin = curTime.getUTCMinutes(),
localTime;
if (curTime.getUTCHours() > baseZoneOffset || (curTime.getUTCHours() == baseZoneOffset && curTime.getUTCMinutes() >= 1)) {
expTime.setUTCFullYear(tmpYear,tmpMonth,tmpDate+1);
expTime.setUTCHours(baseZoneOffset,1,0,0);
} else {
expTime.setUTCFullYear(tmpYear,tmpMonth,tmpDate);
expTime.setUTCHours(baseZoneOffset,1,0,0);
}
localTime = new Date(expTime.toUTCString());
return localTime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment