2

Using C# CSOM and this is SharePoint 2010 - I am attempting to pull the vale of a Date And a time field to my Winform.

If I type in the immediate window listItem[“datetimefield”];

It will return {7/22/2018 3:22:00 AM} Date: Day: DayOfWeek: DayOfYear: Hour: Kind: Millisecond: Minute: Month: Seconds: Ticks: TimeOfDay: Year:

I want to pull the value from Date - what would be the syntax to do such?

EDIT
If it helps this is full syntax I attempted to use. I can connect fine and pull values for other field types, just not a Date and Time field type.

Public static void GetDateTimeData()
{
  using (var ctx = new ClientContext(SPURL))
  {
    List list = web.Lists.GetByTitle(SPSite);
    CamlQuery camlQuery = new CamlQuery();
    camlQuery.ViewXml= strong.Format();

    ListItemCollection items = list.GetItems(camlQuery);
    ctx.Load(list);
    ctx.Load(items);
    ctx.ExecuteQuery();

    foreach (ListItem listItem in items)
    {
        DateTime TargetSD = (dateTime)listItem["maindate"];
        string date = Convert.ToDateTime(listItem["maindate"].ToString();
    }
  }
}
2
  • Just stick it in a DateTime variable May 7, 2018 at 19:36
  • I tried and I was obviously doing it incorrectly as it would just launch my winform. I will show code I try when I get back to my PC
    – HotTomales
    May 7, 2018 at 22:54

2 Answers 2

0

Per my test in my SharePoint 2013 environment, if I get the value from the date and time column in the item, I get the time as the picture below.

enter image description here

Then you could format the time according to the article below.

String Format for DateTime.

2
  • -> what syntax did you use to pull the date? I tried to use DateTime TargetSD = (dateTime)listItem["maindate"]; string date = Convert.ToDateTime(listItem["maindate"].ToString(); but the code above for me and SharePoint 2010 does not actually provide me with the value from the Date and Time field. And yes, I did verify that there actually is a date in the field.
    – HotTomales
    May 8, 2018 at 12:08
  • I just use the code “Console.WriteLine(item["date1"]);”.
    – Amy_MSFT
    May 9, 2018 at 1:27
0

The value is already DateTime. If that's what you want, casting will be enough:

(DateTime)listItem["maindate"]

But if you need as a formatted string, use the toString() method:

DateTime dateTimeValue = (DateTime)listItem["maindate"];
string formatedValue = dateTimeValue.ToString("dd/MM/yyyy");

You may look here if you want to customize the format:

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.