I get the following error when i try to convert to date time.
String was not recognized as a valid DateTime.
cost.b_date = DateTime.Parse(c_date.Text) ;//c_date.Text = 12/28/2012
Then i try
string date = string.Format("{0:yyyy-MM-dd}",c_date.Text);
cost.b_date = DateTime.Parse(date) ;
but i get the same exception how to fix this problem.
Using
string.Format
when the input is a string is pointless.If you know the format of the string, you should use
DateTime.ParseExact
orDateTime.TryParseExact
. For example, for the string you've got, you could use:You should consider:
TryParseExact
to detect user error more easily without an exception.DateTime.TryParse
may be more appropriate.DateTime
to start with, that would be preferable.Try using DateTime.ParseExact.