I have a database where date and time are stored in GMT/UTC to have a uniform time zone reference. The problem is that it isn’t very user friendly to display the GMT/UTC values. In addition, we wanted to get the offset without having the user log in and get preference settings.
var d = new Date();
Note that the method getTimezoneOffset() returns the unit value in minutes. Within the body of the page we call the function during the onload event of the body and used a hidden field for the UTC Offset value that we’ll be getting that we’ll be accessing in our code behind.
<form id="form1" runat="server">
<input type="hidden" runat="server" id="UtcOffset" />
In our code behind, we got the value and put it in a cookie. The alternative was to put it in a session.
protected void Page_Load(object sender, EventArgs e)
if (IsPostBack && Request.Cookies["utcOffset"] == null)
HttpCookie utcOffsetCookie =
new HttpCookie("utcOffset", "0");
utcOffsetCookie.Value = UtcOffset.Value;
At this point, you can retrieve the cookie from any other page and adjust your time values accordingly. Only caveat to this is that the client must hit the page first and then send a post back to get the value of their offset. In our case, we added the code on our login page since we didn’t allow annonymous access to the site.
I should clarify that the value given by dateObject.getTimezoneOffset() is the value added to the local time to get the corresponding value in GMT. I’ve made the mistake of using the value to calculate the local time by simply adding to the current GMT value. Need to remember to negate the value (multiply by -1) to go from GMT to the local time.