Development

How to Get the Time Zone in SharePoint Online Using JSOM

As a collaboration platform, SharePoint Online allows people to share and work on contents in different time zones. It is often required to get the time zone information in context. For example, in a Document Library, you need to transform the created date time in a user-friendly way. The first step is to figure out what time zone it is displaying. There are two types of Regional Settings that specify the time zone:

  • Regional Settings in User Profile Settings
  • Regional Settings under each site

By default, the Regional Settings in the User Profile settings are not set. If the user didn’t select the regional setting in the User Profile Settings page, SharePoint will take the Regional Settings from the site in context. If the user does set the regional settings in the profile page, then SharePoint will ignore the regional setting in the site.

The following are the code to get the time zone with JavaScript code. It fetches the Time Zone from user profile property “SPS-TimeZone” and site’s regional setting “web.get_regionalSettings().get_timeZone()”.

11

The source code is available on GitHub.

https://github.com/pengzhao-perficient/blog/blob/master/GetTimeZoneFromSharePoint/GetTimeZone.js

If you are wondering about the “SPTimeZoneNameToSPTimeZoneId[userTimeZoneValue]” code, you can read my previous blog about “Mappings between SharePoint Time Zones and IANA Time Zones.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up
Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram