2

Using caml query I get Calender Events but Time will be changes..How to get server Time zone ..This return time +5.30 hrs.

  CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View><Query><Where>" +
                              "<DateRangesOverlap><FieldRef Name=\"EventDate\" />" +
                              "<FieldRef Name=\"EndDate\" />" +
                              "<FieldRef Name=\"RecurrenceID\"/>" +
                              "<Value Type=\"DateTime\"><Month /></Value></DateRangesOverlap>" +
                              "</Where></Query></View>";

  ListItemCollection collListItem = oList.GetItems(camlQuery);

            clientContext.Load(collListItem);


            clientContext.ExecuteQuery();
  foreach (ListItem oListItem in collListItem)
            {

                var Title = oListItem["Title"] != null ? oListItem["Title"].ToString() : "";
                var EventDate = oListItem["EventDate"] != null ? oListItem["EventDate"].ToString() : "";
                var EndDate = oListItem["EndDate"] != null ? oListItem["EndDate"].ToString() : "";
                var Location = oListItem["Location"] != null ? oListItem["Location"].ToString() : "";
                var Description = oListItem["Description"] != null ? oListItem["Description"].ToString() : "";

  Calender item = new Calender();

                item.Title = Title;
                item.EventDate = Convert.ToDateTime(EventDate);
                item.EndDate =Convert.ToDateTime(EndDate);
                item.Location = Location;
                item.Description = Description;


                Result.Add(item);
}

In Calender It display date as:means start Time is 3PM to 4PM enter image description here

but it return as: enter image description here

6
  • what is the CAML query you are using ? Jun 15, 2017 at 6:36
  • I add caml query please check it.. Jun 15, 2017 at 6:37
  • Thanks, are you binding the results to html somewhat as new Date(item.EventDate) ? Jun 15, 2017 at 6:44
  • actually I have use it in C#...I bind this to DateTime object Jun 15, 2017 at 6:45
  • can you also add that code in the question ? Jun 15, 2017 at 6:48

2 Answers 2

2

try the below line.

 item.EventDate = TimeZone.CurrentTimeZone.ToLocalTime(EventDate);
1

I had seen many a times people are struggling with DateTime field in SharePoint. SharePoint internally store DateTime filed into UTC format.

So to solve your problem you need to convert the UTC time in accordance with your sever time zone. You can get the web time zone using SPWeb object. Following is the property you need to use.

SPWeb.RegionalSettings.TimeZone

Also if you are using CSOM C# then you need to refer to Web object instead of SPWeb.

3
  • Not able to use SPWeb object..I only worked using Client context object. Jun 15, 2017 at 8:27
  • You can use ClientContent.Current.Web object to get RegionalSettings.TimeZone property. Jun 15, 2017 at 10:57
  • Also i was trying out something with SP page JS global variable and found you can extract web application time zone using JS on page as well using _spPageContextInfo.webTimeZoneData property. Jun 16, 2017 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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