Convert UTC Date returned from SharePoint API to Local time using JavaScript and C#

SharePoint Online stores all date fields in UTC format and convert it to appropriate time zone whenever it is retrieved by a user.

However when using SharePoint API, the date returned is in UTC format and have to be converted to local time.

Using the JavaScript function below the local time can be returned.

self.toLocalDate = function (utc_date) {
//The getTimezoneOffset() value is in minutes
var offset = new Date().getTimezoneOffset();
//offset will be in minutes. Add/subtract the minutes from your date
return utc_date.setMinutes(utc_date.getMinutes() + offset);
}

In C#, the function below can be used
System.TimeZone.CurrentTimeZone.ToLocalTime(date)

Further articles to help understand time zone in SharePoint Online
http://www.techgrowingpains.com/2012/05/sharepoint-time-zone-confusion-2/
https://yetanothersharepointblog.wordpress.com/2013/07/14/timezone-issues-when-working-with-dates-in-sharepoints-rest-services/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s