EDIT: This value: "11/2/2010" does not match the format "dd / MM / yyyy". It corresponds to the format "d / M / yyyy" - for "dd / MM / yyyy" it should be "11/02/2010".
This is why TryParseExact fails for you. You need to choose the correct format.
A value of DateTime has no format. It simply represents the date and time (in the ISO calendar and possibly in different time zones, but that's another matter). It is like an int - it does not represent a "decimal integer" or "hexadecimal integer" - it is just an integer in a certain range. You can format the number as decimal or hexadecimal, but it essentially has no format.
It looks like you should ParseExact it with ParseExact to indicate the format when converting from a text field or, possibly, TryParseExact :
// This is assuming you're absolutely sure of the format used. This is *not* // necessarily the user preferred format. You should think about where your // data is coming from. DateTime date; if (DateTime.TryParseExact(text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) { // Okay, successful parse. We now have the date. Use it, avoiding formatting // it back to a string for as long as possible. }
You must save this value as a DateTime for all purposes except returning it to the user - at this point you may want to use your cultural settings.
In particular, if you store a value in a database, you should not convert it to text and include it in an SQL query that requires trouble. Instead, use a parameterized SQL statement and set it as a parameter value, still as a DateTime .
Jon skeet
source share