Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How to set UTC timezone to a listitem's DateTime field without executing query each time?

This would be slow for many datetime fields and items:

var utc = DateTime.UtcNow;
var localTime = clientContext.Web.RegionalSettings.TimeZone.UTCToLocalTime(utc);
clientContext.ExecuteQuery(); // Slow!
listItem["DateTime"] = localTime.Value;

We are using SP2013

share|improve this question
add comment

3 Answers 3

up vote 2 down vote accepted

Actually you don't have to, time will be saved in Local time when adding list item, so you could just specify time value like this:

listItem["DateTime"] = utc;

Another option would be to retrieve SharePoint time zone and explicitly convert time into Local time using .NET Framework capabilities when saving list item as demonstrated below.

//1.Retrieve SharePoint TimeZone
var spTimeZone = context.Web.RegionalSettings.TimeZone;
context.Load(spTimeZone);
context.ExecuteQuery();

//2.Resolve System.TimeZoneInfo from Microsoft.SharePoint.Client.TimeZone 
var fixedTimeZoneName = sptimeZone.Description.Replace("and", "&"); 
var timeZoneInfo = TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(tz => tz.DisplayName == fixedTimeZoneName);

//3.Convert time
listItem["DateTime"] = TimeZoneInfo.ConvertTimeFromUtc(utc, timeZoneInfo); 
share|improve this answer
    
SharePoint seems to store UTC of the given local time to the database so saving 4pm in UTC+3 timezone would get stored as 1pm. Thus we get 1pm when reading the value (var value = listItem["DateTime"]; // 1pm) –  thomius Jun 15 at 7:14
    
so.. I guess 'listItem["DateTime"] = utc;' would not save utc because sharepoint converts the value to utc before saving. Otherwise your answer seems to work great :-) can we be sure the replacement will always be valid? –  thomius Jun 15 at 17:26
    
Good to hear! Regarding whether convert from UTC or not, tested it with SharePoint Online and it seems it saves it in local time without any additional steps.. –  Vadim Gremyachev Jun 15 at 18:57
    
If you first save a value and then retrieve it, you will get a different value than you inserted: The value you get is converted to UTC, right? (on-premises at least it seems to work like this) –  thomius Jun 15 at 19:01
    
hmm..i get the same value when it saved first and later when it retrieved –  Vadim Gremyachev Jun 15 at 19:16
show 3 more comments

To get local time you can also you basic javascript Date functions.Try below:

var currentDate= new Date();
currentDate.toTimeString();//Result format: 01:59:36 GMT+0530 (India Standard Time)
share|improve this answer
add comment

Ensure that you have set the time zone for the specific web application where you host your list. If it's not set (blank), SharePoint will use UTC.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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