Search code examples
c#visual-studio-2010sharepointsharepoint-2010

how to initialize a string to a date-time field type in SharePoint using c#


I'm creating an app using visual studio in SharePoint that counts months between two dates. and I'm having problem on initializing a value. the code belongs as follows

DateTime _Sdate = ["Sdate"] //this one here does not work 
newDef = jobDef.Items.Add();
newDef["Sdate"] = "01/01/2015"; // i want to initialize this field to _Sdate
newDef["Ndate"] = "07/06/2015";
newDef["Cdate"] = calcDay() // I have this function in my project

anyone can help me please?


Solution

  • I am assuming you have a SpListItem itm which have the column named Sdate. Please see the below code which worked fine for me.

    DateTime _Sdate = Convert.ToDateTime(itm["Sdate"].toString());
    newDef = jobDef.Items.Add();
    newDef["Sdate"] = _Sdate;
    newDef["Ndate"] = "07/06/2015";
    newDef["Cdate"] = calcDay(); 
    newDef.Update();