Skip to content

Instantly share code, notes, and snippets.

@pradeepn
Created May 16, 2013 06:32
Show Gist options
  • Save pradeepn/5589779 to your computer and use it in GitHub Desktop.
Save pradeepn/5589779 to your computer and use it in GitHub Desktop.
TimeZoneOffsetCooke: Useful when working with global website. Store the client UTC time difference in the cookie. Adjust the datetime with the stored time difference from cookie while displaying or manipulating with date and time. The above system works only if you store all the date time in UTC.
var setTimezoneCookie = function () {
var timezone_cookie = "timezoneoffset";
var offset = new Date().getTimezoneOffset();//Require datejs for this method.http://www.datejs.com/
if (!$.cookie(timezone_cookie)) {
// check if the browser supports cookie
var test_cookie = 'test cookie';
$.cookie(test_cookie, true);
// browser supports cookie
if ($.cookie(test_cookie)) {
// delete the test cookie
$.cookie(test_cookie, null);
// create a new cookie
$.cookie(timezone_cookie, offset, { expires: 30, path: '/' });
// re-load the page
//location.reload();
}
}
// if the current timezone and the one stored in cookie are different
// then store the new timezone in the cookie and refresh the page.
else {
var storedOffset = parseInt($.cookie(timezone_cookie));
var currentOffset = offset;
// user may have changed the timezone
if (storedOffset !== currentOffset) {
$.cookie(timezone_cookie, offset, { expires: 30, path: '/' });
//location.reload();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment