Monday, June 7, 2010

SharePoint DateTime Format Conversions

SharePoint 2007 uses ISO8601 DateTime format internally. It understands either of 2 possible notations for DateTime:


  • understandably YYYY being 4-digit year, MM for 2-digit month number, DD for day of month
  • T stands for start of time.
  • Following that denotes the time which could be "hh:mm" (hours-mins) or "hh:mm:ss" (hours-mins-seconds)
  • Z indicates the Coordinated Universal Time (UTC)
On many occassions during MOSS 2007 development, we are required to convert from DateTime to ISO8601 DateTime or vice-versa. So, I decided to post on how this could be achieved.

Convert from DateTime to ISO8601 DateTime (C#):

DateTime date = DateTime.Now;
string isoDate = SPUtility.CreateISO8601DateTimeFromSystemDateTime(date);

When you are trying to insert or modify some DateTime fields to or from a SPListItem, you could use the code given above to format values into ISO 8601.
You can build the CAML query for the SPQuery object as shown below:
Note: Don't forget to add reference of Microsoft.SharePoint.Utilities in your class file before building your project.

Convert from ISO8601 to DateTime (C#):
DateTime date = DateTime.Parse("2010-06-15 00:00:00");
string sysDate = date.ToString("MM/dd/yyyy");

Note: In above example, the ISO DateTime string is converted to DateTime equivalent using DateTime.Parse method.

Also, incase you want to change the Site Collection date format:
  1. Go to "Site Settings" & navigate to "Modify All Site Settings"
  2. Click on Regional Settings under Site Administration
  3. Change the time zone. Save the changes.
This brings us to the end of this lengthy post. Hope it helps you SharePoint folks, goodbye for now.


Anonymous said...

Good post.

Anonymous said...

Thanx a lot :), I wasn't able update DateTime form field properly until i use CreateISO8601DateTimeFromSystemDateTime in ItemAdding event receiver.


Related Posts with Thumbnails