How To Convert a date in ISO 8061 format to a DateTime format!
To get articles like this free in your inbox, subscribe to our newsletter.
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”];
DateTime dtDate=
SPUtility.CreateSystemDateTimeFromXmlDataDateTimeFormat(
properties.AfterProperties[“Date”].ToString());
string sYear = dtDate.Year.ToString();
string sMonth = dtDate.Month.ToString();
properties.AfterProperties[“Code”] =
sYear +
“/” + 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!.
What you should do next…
1. Subscribe to our newsletter:
Subscribe to our newsletter below for the latest news, advice and thought-leadership for software professionals. Or visit our blog to browse our most recent articles.
2. Learn how virtual labs can grow your business:
To learn more about how CloudShare helps software organizations grow revenue, increase efficiency and improve quality, visit our resources page. You’ll be able to browser dozens of valuable white papers, eBooks, webinars, case studies, and brochures.
3. Get a FREE, no obligation demo:
Discover just how easy it is to create your cloud environment—in minutes! One of our friendly virtual labs experts will be happy to:
- Show you the platform in action
- Calculate pricing for your business
- Set you up with a 14-day free trial
- Answer any questions you have
- No pressure or obligation