Time based query issue in SharePoint Framework
Issue:
Whenever you are making query to the SharePoint online using the REST API and your query is based on the time stamp, remember that SharePoint treat your time as the GMT timezone. So if you want to fetch information based on local time zone which we usually do, always set the offset and also include the daylight saving.
The same is true even you are trying to put in some data to the SharePoint. You will be saving the local timezone but SharePoint will consider it as GMT time zone , and there you won’t be able to save/retrieve the correct records based on the time zone.
Solution
Solution is simple. SharePoint Framework webpart/extension offers this capability OOB, but there were no articles i was able to find.
// will get the final offset in minutes
let finalOffset: number = this.context.pageContext.web.timeZoneInfo.offset + this.context.pageContext.web.timeZoneInfo.daylightOffset
// Now set the required time in the GMT time zone
let convertedDateTime: Date = new Date(new Date().setMinutes(finalOffset));
Now whether you query the SharePoint or POST some data in SharePoint it will always be correct now the retrieving and posting will be in the same time zone.
Hope this article will save lot of your time figuring out the time difference of the value you are saving and you are retrieving which was mainly due to the time zone and daylight saving.
Happy Coding
Sumit Kanchan
Comments
Post a Comment