TimeZones and how SharePoint handles timestamps
We recently had an issue logged in the PnP Modern Search repository regarding why the date filter values were "wrong".
Actually, the date filter values were correct as they were offset to match the current users Time Zone, which can be pretty hard to understand, hence this summery:
Disclamer: I am in Denmark, currently in UTC+2 (summertime)
Each time a timestamp is saved in SharePoint it is saved in UTC0, so when I upload a document at 13.45 local time the timestamp saved in the backend will read 11.45.
But in the Document library it clearly shows 13.45, what gives?
Each time the SharePoint Front End displays a timestamp it is offset to the local timezone, so my colleague in Los Angeles looking at the same document will see the timestamp as 04.45 (9 hours behind my timezone).
However, when you get the timestamp from an API you get the time in UTC0 !!!!
So, if I am looking for documents that was created between 4 April and 10 April I must answer the question: from which timezone do I define when 4 April begins?
If I lived in New Zealand(UTC+12) 4 April starts a lot earlier than it does here in Denmark(UTC+2).
So, when somebody asks you to set up a PowerShell script to pull all Contracts (content type) created on 10 April, you have to ask is the offset should be based on the Time Zone of the requestor or something else.
In PnP Modern Search any time-based query is automatically offset to match the Time Zone of the current user.
When I query for something created between 4 April and 10 April the query is changed to:
creased between 3 April 22h00m and 9 April 22h00m.
The reason is that when 4 April started in Denmark the time was 3 April 22h00m UTC0 which is the values in the backend.
You can find your current TimeZone info by navigating to a classic SP Page, hitting F12, go to the Console tab, enter _spPageContextInfo and hit Enter, look for webTimeZoneData:
Time Zones might be hard to deal with, but I guess it is there to stay :-)
Comments