5

I am working on a solution (on SharePoint Online) which is used by users in different timezones. SharePoint site is configured for "W. Europe Standard Time".

Following occurs:

  • A user from US creates a list item using SharePoint standard form and fill in a date only field with September 15.
  • When the user opens the item and views the properties it shows September 15.
  • There is custom view build that retrieves the list item data using JSOM. Item is retrieved with "getItemById" and values are read with "get_fieldValues".
  • I, as a user from Europe, see in both the custom solution and in SharePoint UI September 15.
  • User that created the items sees two different values. September 15 in SharePoint UI and September 14 in the custom solution (which retrieves the info via JSOM)

I have looked at how the data is returned when get_fieldValues is used and it look like this for a US user: Sun Aug 14 2016 17:00:00 GMT-0500 (Central Daylight Time). I inspected the response body of the call and can see that it return a UTC time which is then translated to this local time in the process of reading the value. Raw response for a date fields looks like this: Date(1469656800000).

Also tested to see how REST API returns values and there I see that date values are turned in UTC like "2016-08-14T22:00:00Z". One possible solution for my problem is to switch to REST api and show the dates in web region, this is what SharePoint appears to be doing in his own UI.

But I don't want to re-build the data retrieval process and am looking for a way to use JSOM and somehow workaround/solve this issue. Any ideas how to do this?

  • Have you found a solution to this? – Heinrich Jun 28 '18 at 0:53
0

If you are able to incorporate a third-party JavaScript library into your solution, then you can use moment.js with the moment-timezone extension to correct for this issue. As an example:

moment.tz("2016-08-14T22:00:00Z","Europe/Berlin").format("YYYY-MM-DD") // "2016-08-15"

The value "Europe/Berlin" is a reasonable equivalent of "W. Europe Standard Time", including its DST rules. If you need a solution that doesn't hard-code the time zone, then take a look here.

Also understand that the root cause of the problem is that SharePoint is storing the date-only value as if it were a date-time at midnight in the server's time zone. Therefore, another way to prevent this issue would be to have the server's time zone set to UTC, and to avoid UTC-to-local conversion on the client. Of course, this could affect other things if change it after data was already written, so be careful if you go down that route.

  • Thx for your suggestion Matt. I was also looking at moments.js as a possible solution for converting the dates. Can moments.js be used to convert a date from one timezone to another as I am receiving the date, when retrieved with JSOM, in the users timezone instead of UTC? – Suleyman Aug 4 '16 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.