String was not recognized as a valid DateTime " format dd/MM/yyyy"
Use DateTime.ParseExact
.
this.Text="22/11/2009";
DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);
You need to call ParseExact
, which parses a date that exactly matches a format that you supply.
For example:
DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
The IFormatProvider
parameter specifies the culture to use to parse the date.
Unless your string comes from the user, you should pass CultureInfo.InvariantCulture
.
If the string does come from the user, you should pass CultureInfo.CurrentCulture
, which will use the settings that the user specified in Regional Options in Control Panel.