This time I would like to share a quick tip with you that allows you to solve typical problems you can find when working with dates in your SharePoint code: how to convert a date in ISO 8061 format to a .NET DateTime format. For instance, this situation happens when you have to update a list item field inside the code of a SharePoint event receiver and the field type to process is just a SharePoint DateTime field.
In order to work with SharePoint Date field types, you must know that the values stored in this fields are returned as an ISO 8061 string when you programmatically access to the content field. This means that you will need to convert string to the right format between doing any operation with it. Fortunately, SharePoint SPUtil class provides great methods that allows to deal with this requirement…in this particular scenario, you can use the CreateSystemDateTimeFromXmlDataDateTimeFormat() method to format the ISO 8061 string to a valid date format:
var dtDateAux = properties.AfterProperties[“Date”];
string sYear = dtDate.Year.ToString();
string sMonth = dtDate.Month.ToString();
“/” + sMonth;
As you can see in the code above, this method allows to create .NET DateTime type using this method and passing the SharePoint Date field as parameter. As soon as you have your .NET DateTime field, you can start to do operations with it.
And that’s all about how to convert a date in ISO 8061 format to a DateTime format. Happy CloudSharing!.