Ian Purton

Ian Purton’s Notes

Place Timezone information into a cookie with JavaScript

  • PublishedMarch 3rd, 2006
  • Authorian.purton
  • CategoriesJavascript

This JavaScript will pick up the users timezone offset and place it nto a cookie. This is useful for when you want to diaply times in your web application taking the users location into account.

As the timezone is placed in a cookie it can be extracted by any serverside scripting technology. Javascript returns the timezone information in minutes so the script converts it into hours.

// By default, getTimezoneOffset returns
// the time zone offset in minutes
setCookie("timezone", new Date().getTimezoneOffset()/60);

function setCookie(name, value, expires, path, domain, secure)
{
	document.cookie= name + "=" + escape(value) +
		((expires) ? "; expires=" +
		expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

"Place Timezone information into a cookie with JavaScript" was published on March 3rd, 2006 and is listed in Javascript.

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Place Timezone information into a cookie with JavaScript: 1 Comment

  1. Sachin Gedam

    Its really a useful way to get timezone information.. but there is a problem with this solution…there are many time zones which shares same GMTOffset, so how can identify exact time zone on server side based on GMTOffSet.

    For example:
    Beijing
    Perth
    Taipei
    Kula Lumpur

    these time zone sharing same GMT Offset (+8:00), so when i receive GMTOffset than at server side i may not be able to get exact time zone.

Leave Your Comment

(required)
(required)
 

Subtraction Wordpress Theme by Ian Purton