0

I have custom list in sharepoint 2016 on-premises. There are several columns in DateTime type. I need to query this list in C#, but when I run my query I get wrong dates - I get always the day before the date which I see in the list. For ex: I have a column HiringDate in the list and record with value "10/2/2018" (MM/dd/yyyy). The caml query in c# code returns it "2018.10.01. 22:00". Here's my code:

            List oList = context.Web.Lists.GetByTitle(listName);
            ListItemCollection items = oList.GetItems(new CamlQuery());
            FieldCollection fcol = oList.Fields;
            context.Load(items);
            context.Load(fcol);
            context.Load(oList);
            context.ExecuteQuery();

            string[,] list = new string[items.Count, 3];

            for (int i = 0; i < items.Count; i++)
            {

                var itemValue = items[i]["HiringDate"];
                Console.WriteLine(itemValue);
                if(itemValue != null && itemValue.GetType() == typeof(DateTime))
                {
                    list[i, 1] = ((DateTime)itemValue).ToString("yyyy.MM.dd");
                    Console.WriteLine(list[i, 1]);
                }


            }

I am new to c# and sharepoint. Why I receive wrong date and how can I fix it?

Thanks!

0

SharePoint store datetime as UTC format, you can convert the datetime to site time zone time with below sample code:

var localTime = clientContext.Web.RegionalSettings.TimeZone.UTCToLocalTime((DateTime)_Item.FieldValues["Created"]);
clientContext.ExecuteQuery();

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.