Skip Ribbon Commands
Skip to main content
Home
January 06
String was not recognized as a valid date time

Parsing the string having datetime with datetime format different than regional locale settings gives very common error, “String was not recognized as a valid DateTime.”

SharePoint is enough intelligent to understand the format of the date time from the regional settings and doesn’t give error while parsing a string of different locale format to convert it to date time, only if you provide the required information to SharePoint.
 
What do I mean by that?
It’s seldom, e.g. where we receive the date time from a site collection with regional locale settings is English (United Kingdom) and code to parse that string to date-time getting executed on a site collection with regional locale settings is English (United States).
Date time format with locale settings English (United Kingdom):  24/2/2010 6:32:10 PM
Date time format with locale settings English (United States):  2/24/2010 6:32:10 PM
 
The code generally fails in such a situation:
DateTime
.Parse("24/2/2010 6:32:10 PM")
Then what is expected, what I said earlier, provide correct information to SharePoint.
SharePoint has method to handle such situations and the code is:
SPUtility.ParseDate(web, "24/2/2010 6:32:10 PM",SPDateFormat.DateTime,true);
 
In this, what you are providing to SharePoint :
1) The SPWeb object of the site from which the date time in UK format is received, which gets the regional locale setting of the source site collection and parse the string to datetime without error. :)
2) Here time format in date string is taken as per the source site collection (i.e. 12 hrs) in your case, check the settings of Time Format if 12/24 and accordingly use the datetime string for successful testing, in case of actual code it will come in rspective format.
3) SPDateTimeFormat use correctly, as per receiving date's format (date to be parsed).
4) True/False, to decide, if want the date after parsing in UTC format or not. 
 
Now can play with the date time from different regional, locale settings without worry.!!!
January 06
Sharepoint 2010 RTM Content Types Precaution

It has always been an excitement to work on Beta’s and RTM’s of any product.
These days I have been working on SharePoint 2010 based products. While working on Content Types in SharePoint 2010 I faced few challenges,
I refered to the link : http://msdn.microsoft.com/en-us/library/ms460224.aspx to work on content types either programmatically or through xml’s.
In 2010, if you create content type (SPContentType) and associate or add that content type to sharepoint list (SPList) on activation of feature. Now in deacivation of feature if I have written logic to delete the same SPList and later if I try to delete the content type from site then it gives error saying content type is in use.
To survive from this couple of tips,
  1. Always check the usage of the content type before deletion
  2. Always remove the content type association from list and then delete the list
  3. In case if you have forgotten to remove the content type association from the list and you have already deleted the list and now if you want to delete the content type, use recycle bin to restore a list ( tip from twitter.com/danmc) and then first remove the association of content type from th list and then delete the content type.
I have always suffured being an aggressive developer especially with RTM’s and Beta’s…still love microsoft’s products :)
Success!!!!

 

 About this blog

 
About this blog
Welcome to SharePoint Blogs. Use this space to provide a brief message about this blog or blog authors. To edit this content, select "Edit Page" from the "Site Actions" menu.
 

 Most Discussed

 
String was not recognized as a valid date time 0
Sharepoint 2010 RTM Content Types Precaution 0
 

 Most Recent

 
String was not recognized as a valid date time 865 Days Ago
Sharepoint 2010 RTM Content Types Precaution 865 Days Ago
 

 Information