kamagra how much to take

Remember When Using SPUtility.FormatDate To UTC (Extension Method)

This post is also because people said they required a more reusable extension method than here that tapped into SharePoint. Tyrants!

While I was doing a code review that tapped into some of the calendar segments of SharePoint I found this issue come up. When passing date parameters into SPUtility.FormatDate it will expect that it will be in UTC, which in my particular case, as well as I would assume others, was inaccurate since it was stored as local time. This can be overcome by using the methods provided by the SPWeb.RegionalSettings.TimeZone class, which will allow proper analysis to occur on SPWeb objects to support suitable conversion.

Wouldn’t it just be helpful if all this nonsense was taken care of, and you could just call a SPListItem.GetDateAsIso and pass in the SharePoint field name you would like to query against? Since this is going to extending the API and reused heavily, it makes sense to put this into an extension method (and for my own devious purposes of expanding on the previous post)! Since ISO8601 format is common in regulatory agencies, this is the return format I am going to target.

In the below Extensions class, there is the GetDateAsIso method which returns a DateTime object taking in 2 supplementary parameters outside of type declaration: the SharePoint field name, and a default DateTime value. We perform some argument checking, and then call Convert.ToDateTime on the specific SharePoint field. After, a combination of SPWeb.RegionalSettings.TimeZone.LocalTimeToUTC / UTCToLocalTime is used to ultimately populate the SPUtility.FormatDate method. The return should be a date string representative of the ISO8601 format, therefore in order to return this as a DateTime object Convert.ToDateTime is used.

  1. public static class Extensions
  2. {
  3. public static DateTime GetDateAsIso(this SPListItem listItem, string fieldName, DateTime defaultVal)
  4. {
  5. SPWeb web = listItem.Web;
  6. try
  7. {
  8. if (string.IsNullOrEmpty(fieldName))
  9. {
  10. return defaultVal;
  11. }
  12. if (listItem[fieldName] == null)
  13. {
  14. return defaultVal;
  15. }
  16. DateTime curDate = Convert.ToDateTime(listItem[fieldName]);
  17. DateTime regionDate = web.RegionalSettings.TimeZone.UTCToLocalTime(web.ParentWeb.RegionalSettings.TimeZone.LocalTimeToUTC(curDate));
  18. return Convert.ToDateTime(SPUtility.FormatDate(web, regionDate, SPDateFormat.ISO8601));
  19. }
  20. catch (Exception exception)
  21. {
  22. Debug.Write(exception.Message);
  23. }
  24. return defaultVal;
  25. }
  26. }
  28. public static void TestingExtensionMethod()
  29. {
  30. SPList list = SPContext.Current.Web.Lists["My List"];
  31. foreach (SPListItem item in list.Items)
  32. {
  33. item.GetDateAsIso("My Field", DateTime.Now);
  34. }
  35. // Do whatever you want!
  36. }

No Comments


  1. Links (10/1/2008) « Steve Pietrek - Everything SharePoint - [...] Remember When Using SPUtility.FormatDate To UTC (Extension Method) [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>