Posts Tagged ‘timezone’

Sharepoint timezone hell

March 20, 2009


I’m currently participating in deploying an international Sharepoint intranet which hosts teamsites where users can track tasks and knowledge. Our infrastructure is running in the GMT+1 timezone but not all of our users are in that timezone. Since Sharepoint is using web technology all users see by default GMT+1 times, which is very annoying when you are working from within another time zone.

Site Time zones

Sharepoint allows owners, of a site, to define the timezone they want for their site. This in order to see correct date and time stamps on items (eg on created and modified fields). It is recommended that the owners of the site define the timezone based on the timezone of the users who most frequently use the site.

User time zones

If the visiting user is in a different timezone as the site he can personalize the display of dates and times so it aligns with his timezone. But they need to set this for each site and they have no idea that there is a difference.

Time zone check webpart

In order to warn users that the site they are visiting does not run in the same timezone as theirs, I created a small webpart. The webpart generates some javascript that checks if the local timezone bias is different than the “context timezone” bias. The “context timezone” represents the timezone as experienced by the user visiting the site.

The code for building the webpart (this code is functional but not at the level you would expect to run on a production environment):

protected override void Render(HtmlTextWriter writer)
//initiate objects
SPWeb oWebsite = SPContext.Current.Web;
SPRegionalSettings oContext = SPContext.Current.RegionalSettings;
//write javascript code in the webpart
writer.Write("<script language='JavaScript'>");
writer.Write("var d = new Date();");
//Edit below lines to print the server and client time offset
//writer.Write("document.write(\"Server time offset:" + (oContext.TimeZone.LocalTimeToUTC(DateTime.Now) - DateTime.Now).Hours.ToString() + "<br>\");");
//writer.Write("document.write(\"Client time offset:\" + d.getTimezoneOffset());");
//below line gives problems with daylight savings
//writer.Write("document.write(\"Server bias offset:" + oContext.TimeZone.Information.Bias + oContext.TimeZone.Information.DaylightBias + "<br>\");");
 //check if the timezone on the local machine is equal to the current WSS context timezone
writer.Write("if (d.getTimezoneOffset()/60 == " + (oContext.TimeZone.LocalTimeToUTC(DateTime.Now).Hour - DateTime.Now.Hour).ToString() + ")");
//Enable the below line if you want show an ok message when the timezones match
//edit below section with the message for mismatched timezone settings
writer.Write("document.write(\"<b><font style='color:red'>Warning!</font></b><br/>\");");
writer.Write("document.write(\"The website is running in:" + oContext.TimeZone.Description +"<br/>\");");
writer.Write("document.write(\"click <a href='_layouts/regionalsetng.aspx?Type=User'>here</a> to adapt your view\");");


The above code allows you to build the below webpart using Visual Studio.



On request I provide the webpart in a Sharepoint solution allowing you to install it on your environment.