Detecting the Current SharePoint User’s Regional Settings
Brian McCullough (@bpmccullough) pinged me on Twitter today with a question:
. @sympmarc is this still only option fir getting users regional settings client side? Have you come across any other alternatives?
— Brian McCullough (@bpmccullough) October 3, 2017
Brian was asking about an old blog post of mine where I basically screen scraped the data: Detecting the Current User’s Regional Settings with jQuery. It worked fine in the old days, but I didn’t think it would work anymore on SharePoint Online.
It was bugging me that I couldn’t remember the answer, so I went digging into some SharePoint pages to see what I could see. In two different SharePoint Online tenants, I saw that the _spPageContextInfo object has the following properties:
"preferUserTimeZone": false, "userTimeZoneData": null, "userTime24": false, "webTimeZoneData": { "Description": "(UTC-05:00) Eastern Time (US and Canada)", "Bias": 300, "Id": 10, "DaylightBias": -60, "DaylightDate": { "Year": 0, "Month": 3, "DayOfWeek": 0, "Day": 2, "Hour": 2, "Minute": 0, "Second": 0, "Milliseconds": 0 }, "StandardBias": 0, "StandardDate": { "Year": 0, "Month": 11, "DayOfWeek": 0, "Day": 1, "Hour": 2, "Minute": 0, "Second": 0, "Milliseconds": 0 } }, "webTime24": false
As I recall, if the userTimeZoneData is not null, then the user has set their time zone themselves. If it is null, then the user is going along with the webTimeZoneData. I don’t have an example, but I’m pretty sure you can also check preferUserTimeZone to see if the user wants to have her own time set.
Note that there are lots of other goodies in _spPageContextInfo as well. The other properties which are pertinent to time zones are:
- userTime24 – Does the user prefer 24 hour (military ) time?
- webTime24 – Does the Web display 24 hour (military ) time?
The TimeZoneData properties are also quite rich, showing the time zone description, the bias from GMT, Daylight Savings Time bias (usually 60 mins, but sometimes 30 mins), when DST starts and ends.
<update date=”2017-10-04″>
Bryan Mathews (@brmathews) reminded me on Twitter that there is a REST endpoint that provides some of this information as well.
Check /_api/web/RegionalSettings/TimeZone pic.twitter.com/stNiiyd7PG
— Bryan (@brmathews) October 3, 2017
If you use the /_api/web/RegionalSettings/TimeZone endpoint, you’ll get back information like this:
<entry xml:base="https://sympraxis.sharepoint.com/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"> <id>https://sympraxis.sharepoint.com/_api/web/RegionalSettings/TimeZone</id> <category term="SP.TimeZone" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/> <link rel="edit" href="web/RegionalSettings/TimeZone"/> <title/> <updated>2017-10-04T13:27:39Z</updated> <author> <name/> </author> <content type="application/xml"> <m:properties> <d:Description>(UTC-05:00) Eastern Time (US and Canada)</d:Description> <d:Id m:type="Edm.Int32">10</d:Id> <d:Information m:type="SP.TimeZoneInformation"> <d:Bias m:type="Edm.Int32">300</d:Bias> <d:DaylightBias m:type="Edm.Int32">-60</d:DaylightBias> <d:StandardBias m:type="Edm.Int32">0</d:StandardBias> </d:Information> </m:properties> </content> </entry>
This is XML, but the data you’ll get back in JSON is the same. Note that there isn’t as much details here as in the _spPageContextInfo, but this endpoint is available to you in any page.
</update>
Note that depending on the version of SharePoint you’re running, your mileage may vary.
Does anybody really know what time it is?