I am trying to convert my string formatted value to date type with format dd/MM/yyyy
.
this.Text=\"22/11/2009\";
DateTime date = DateTime.Parse(
Parsing a string representation of a DateTime is a tricky thing because different cultures have different date formats. .Net is aware of these date formats and pulls them from your current culture (System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat
) when you call DateTime.Parse(this.Text)
;
For example, the string "22/11/2009" does not match the ShortDatePattern for the United States (en-US) but it does match for France (fr-FR).
Now, you can either call DateTime.ParseExact
and pass in the exact format string that you're expecting, or you can pass in an appropriate culture to DateTime.Parse
to parse the date.
For example, this will parse your date correctly:
DateTime.Parse( "22/11/2009", CultureInfo.CreateSpecificCulture("fr-FR") );
Of course, you shouldn't just randomly pick France, but something appropriate to your needs.
What you need to figure out is what System.Threading.Thread.CurrentThread.CurrentCulture
is set to, and if/why it differs from what you expect.