Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sharepoint save a date to a date field

Tags:

c#

sharepoint

How can you save a date in sharepoint programatically?

I have a list with a date field and want to save a date into that field and a regular DateTime field isnt working.

like image 270
raklos Avatar asked Aug 04 '09 14:08

raklos


People also ask

How does SharePoint store date?

Please note that SharePoint stores all times in UTC format. So when User from Pacific stores data as 5 PM their time, SharePoint writes it in UTC format. If you are intending it to convert it, you can convert it to Pacific time.

How do you add a date and time column in SharePoint?

Under the List tab, click on the “Create Column” button in the ribbon. Provide the Name to your new column, specify the type as “Date and Time” Fill in other optional values and click on “OK” to create a date and time column in the SharePoint Online list.


2 Answers

You should be able to save a standard .NET DateTime object into a SPFieldDateTime, like so:

using (SPSite site = new SPSite("http://YOUR URL"))
{
  using (SPWeb web = site.OpenWeb()) 
  {
    SPList list = web.Lists["news"];

    SPListItem item = list.Items.Add();
    DateTime dt = DateTime.Now;

    item["Title"] = "Test";
    item["Expires"] = dt;

    item.Update();
  }
}
like image 199
Magnus Johansson Avatar answered Sep 24 '22 21:09

Magnus Johansson


you need to convert the date to iso format

myListItem["nameofmydatetimefield"] = SPUtility.CreateISO8601DateTimeFromSystemDateTime(mydatetimeobject);
like image 34
Venketesh Avatar answered Sep 23 '22 21:09

Venketesh