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

  • what is the CAML query you are using ? – Gautam Sheth Jun 15 '17 at 6:36
  • I add caml query please check it.. – Sarika Koli Jun 15 '17 at 6:37
  • Thanks, are you binding the results to html somewhat as new Date(item.EventDate) ? – Gautam Sheth Jun 15 '17 at 6:44
  • actually I have use it in C#...I bind this to DateTime object – Sarika Koli Jun 15 '17 at 6:45
  • can you also add that code in the question ? – Gautam Sheth Jun 15 '17 at 6:48
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.

  • Not able to use SPWeb object..I only worked using Client context object. – Sarika Koli Jun 15 '17 at 8:27
  • You can use ClientContent.Current.Web object to get RegionalSettings.TimeZone property. – ThinkB4Code Jun 15 '17 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. – ThinkB4Code Jun 16 '17 at 7:19

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.