SharePoint: Assigning values to DateTime fields

SharePoint list items have a tricky way how to assign values to DateTime fields in your code. It is possible to assign value of DateTime type to this field but it also possible to assign a string. This string is a little bit mysterious. You may get errors with one dates but some dates will work perfect. So what’s the trick?

If you want to assign date as a string to SharePoint’s DateTime type field then you must format this string as an invariant date. The following line of code explains better what I mean.

item["MyDate"] = DateTime.Now.ToString(DateTimeFormatInfo.InvariantInfo);

If you guarantee that dates are in invariant format then you have no problems when assigning values to DateTime fields. Remember that invariant format is:

MM/dd/yyyy HH:mm:ss

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    One thought on “SharePoint: Assigning values to DateTime fields

    • January 19, 2009 at 4:06 pm
      Permalink

      How can assigning values to DateTime field and cannot select or enter a date before Today?

      If Today= 19 Jan, 2009 (2009/1/19)

      user cannot select or enter 1~18 Jan, 2009 , only can enter grant than 19 Jan, 2009.

    Leave a Reply

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