i have tried every possible solution online but can’t figure out why i am still getting this error
“string was not recognized as valid Date Time”
the funny thing is that i am converting 2 date times, one is cell 3 in my gridview and the other one is cell 7 but the Cell 3 works fine and but Cell 7 does not want to convert and both of them are the same data types!!. I am pulling these data from my sql server table and the data types are varchar but the format should be mm/dd/yyyy.
I have tried this method but it did not work:
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox cb = (CheckBox)gr.FindControl("chkItem");
if (cb.Checked)
{
DateTime ExpectedSubDate = DateTime.Parse(gr.Cells[3].Text);
DateTime strTargetDate = DateTime.ParseExact(gr.Cells[7].Text, "MM/DD/YYYY HH:MM:SS TT", System.Globalization.CultureInfo.InvariantCulture);
I have also tried this method but did not work either
DateTime strTargetDate = DateTime.Parse(gr.Cells[7].Text);
I have also tried this way but did not work either:
DateTime strTargetDate = DateTime.ParseExact(gr.Cells[7].Text, "MM/DD/YYYY", System.Globalization.CultureInfo.InvariantCulture);
The format string is case sensetive, otherwise it can’t know where you want it to parse as months or as minutes.
For a date and time, for example
"11/15/2012 02:26:35 PM":For just a date, for example
"11/15/2012":Ref: Custom Date and Time Format Strings