Dealing with Multiple Time Zones in SharePoint 2010

Organizations that deploy SharePoint farms often have employees in different countries, or at least in different Time Zones. While people in the US (which spans 4 time zones) are pretty comfortable with translating between time zones all the time, the same cannot be said for everyone. Trying to translate between Pacific Time and Middle European Time is just painful, especially since the daylight savings time starts and end at different dates.

With SharePoint 2010 you get the tools to convert the time according to the users time zone. There are two types of Regional Settings: Each Site (SPWeb) has RegionalSettings that specify the Time Zone (and Locale, Calendar etc.) for that site. This is useful if you have sites that are predominately used by people in one time zone. The second type of Regional Settings are the one the user (SPUser) can set (My Settings - My Regional Settings). Those are the same settings as the ones on SPWeb, but each user can specify their own setting.

When storing Dates in code, you have two options:

  • Store the time in local time of the Web and use DatesInUtc = true on a SPQuery to get it back as Utc
  • Store the time in Utc and to not use DatesInUtc on SPQuery

What does that mean? As said, each SPWeb has it's own Regional Settings. Let's assume you have a date of 2010-06-14 15:00:00.

If the TimeZone of the SPWeb is Pacific Time (GMT-8) and you query the List using SPQuery, you get back this date. If you however set DatesInUtc = true on the SPQuery, you get back 2010-06-14 22:00:00. SharePoint doesn't know if 15:00:00 was already UTC, so using DatesInUtc may translate a date twice.

The caveat here is that when storing dates, you would normalize them either to UTC or to the Local Time of the Web. What would you do if some employee from Texas (which runs on Central Time, GMT-6) enters 2010-06-14 15:00:00? You would need to store it either as GMT-8 (so the time becomes 13:00:00) or as UTC (22:00:00).

Needless to say, I prefer to store all dates as UTC if the list isn't visible to the user directly. Then when querying the list through Code, I can just convert the time to whatever the user's timezone is:

var user = SPContext.Current.Web.CurrentUser;
// Always perform a Null-Check on SPUser.RegionalSettings
if (user.RegionalSettings != null)
{
    return user.RegionalSettings.TimeZone.UTCToLocalTime(listDateUtc);
}
else
{
    // User didn't set a time zone, so use the one from the Web
    return SPContext.Current.Web.RegionalSettings.TimeZone.UTCToLocalTime(listDateUtc);
}

Overall, the option for people to set their own timezones independently from the SPWeb is a fantastic and long needed addition. On the other hand, it does make dealing with times a bit more complex.

If the list is visible to the user, you may need to normalize the times differently (for example, use user.RegionalSettings.TimeZone.LocalTimeToUTC to convert a user time to UTC and then SPWeb.RegionalSettings.TimeZone.UTCToLocalTime to convert the time to the Web-Time).

If you do build custom pages that make use of the Microsoft.SharePoint.WebControls.DateTimeControl then you can just use UseTimeZoneAdjustment="true" on it to have it automatically convert to UTC and back (SelectedDate will be UTC when accessed through code, but the User's/Web's time when rendered).

Comments (6)

Mark NashJune 11th, 2010 at 17:50

Good update!

John GodmanAugust 26th, 2010 at 17:25

This is great -- we are a large global enterprise with thousands of users all over the world. Unfortunately, after some spot-checking it appears that everyone's time zone is set to the same zone as the server. Aside from sending out a "how to" message and asking all users to make this change, is there any way to do it administratively?

I fear most users will not bother with it until dates/times start showing up wrong, and then they'll call the help desk and we'll still have to walk them through the setting.

mstumAugust 27th, 2010 at 08:18

I don't think there is a way to do it through any of the Admin Pages. You could write some code (either C# or some PowerShell) that basically gets all SPUsers from a Site Collection and updates their Regional Settings based on some Criteria (e.g., office location), but I don't think there is anything out of the box.

mstumAugust 27th, 2010 at 08:21

Actually if you go to Central Administration => Service Applications => User Profile Service Application => Manage User Profiles, then search for one and then click Edit, you can change the Time Zone. I'm not sure if that's only for the MySite though, and it's tedious for thousands of users.

DeathRiderDecember 19th, 2011 at 06:18

How can we change the locale from en-US to en-UK through coding?

mstumDecember 20th, 2011 at 08:46

Locale of what? The Web? The User? There is a RegionalSettings property on any of those. Don't know off-hand what the correct values are, but I know it's reasonably simple to change, I think you just set the LocaleId.