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.
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.
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.
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();
}
}
you need to convert the date to iso format
myListItem["nameofmydatetimefield"] = SPUtility.CreateISO8601DateTimeFromSystemDateTime(mydatetimeobject);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With