problem while converting object into datetime in c#

Posted by Lalit on Stack Overflow See other posts from Stack Overflow or by Lalit
Published on 2010-05-25T09:14:10Z Indexed on 2010/05/25 9:31 UTC
Read the original article Hit count: 199

Filed under:
|
|

Hi, I am getting string value in object let say "28/05/2010". While i am converting it in to DateTime it is throwing exception as :

String was not recognized as a valid DateTime.

The Code is:

object obj = ((Excel.Range)worksheet.Cells[iRowindex, colIndex_q17]).Value2;
Type type = obj.GetType();
string strDate3 = string.Empty;
double dbl = 0.0;

if (type == typeof(System.Double))
{
    dbl = Convert.ToDouble(((Excel.Range)worksheet.Cells[iRowindex, colIndex_q17]).Value2);
    strDate3 = DateTime.FromOADate(dbl).ToShortDateString();
}
else
{
    DateTime dt = new DateTime().Date;
    //////////dt = DateTime.Parse(Convert.ToString(obj));
    **dt = Convert.ToDateTime(obj).Date;**
    strDate3 = dt.ToShortDateString(); 
}

The double star "**" line gets exception.

© Stack Overflow or respective owner

Related posts about c#

Related posts about exception