Often times for simple SharePoint customization over the years I have used JavaScript to display custom output of List data for clients. Usually this involves calendars or also showing the date of events. While doing this I ran into the common issue many developers see where some dates appear to be off by a day (one day too early). Usually this shows up when the calendar allows for All Day events. But then when you go and check the data of the list everything looks correct. So where is this offset coming from?

The key to this head scratcher is that SharePoint stores the dates in UTC. Thus depending on the UTC offset of your visitors, the dates could be a bit random and confusing. With SharePoint 2013 Microsoft has added some handy tools for doing this in the new SP.js include with the SP.TimeZone.utcToLocalTime() and SP.TimeZone.localTimeToUTC() functions. Read more here: http://msdn.microsoft.com/en-us/library/jj245652%28v=office.15%29.aspx

But for those of us still supporting and working in SharePoint 2010 systems, how can we accomplish this without jumping through too many extra hoops? The answer is quite simple. All we really need is to determine the timezone offset of the current browser. The JavaScript date object has a getTimezoneOffset() method that returns the offset in minutes. Here is a simple date parsing function I have used for custom calendars that takes the raw string value returned from SharePoint and returns a JavaScript date object for easier use:

 

Like this post? Share it!