How to Read a Date Field from Excel Cell?

Hello,

I am trying to read an Excel cell, which is supposed to be a date field. I need to validate it, and cast it to a DateTime field.

This code returns me the format,



string format = ((Excel.Range)dataRange.Cells[i, 1]).NumberFormat.ToString();


 


and this code return the value of the cell.



string value = ((Excel.Range)dataRange.Cells[i, 1]).Text.ToString();


 



I can use format along with a DateTimeFormatInfo object to parse the string, and convert it to DateTime.  But, since the format can be changed by user, and somehow the format that I get is not neccessary clean (i.e. m/d/yyyy hh:mm both "m" in the format are lowercase) I have to play with it and make it clean.

And now my question:

Is there any standard way to get the DateTime object from the content of a Cell

Thank you.



Answer this question

How to Read a Date Field from Excel Cell?

  • Rama Satya Jagan K

    Did you resolve this issue Just curious as there has been no activity in this thread since 11/15/05. If I don't hear from you within a week, I'll just consider the matter closed.

    Thanks!

    Mike Hernandez
    Community Program Manager
    VSTO Team


  • kjaghblb

    Convert.ToDateTime itself uses the DateTime.Parse() and what does it have to do with my question
  • CSWong

  • FranzCarl

    Hi,

    this is the code you should use:

     

    DateTime dt = DateTime.FromOADate((double)((Excel.Range)dataRange.Cells[i, 1]).Value2);

    The result will be accurate with the exception of some dates in the beginning of the year 1900.

    HTH,

    iouri

    ---

    This posting is provided "AS-IS" and confers no rights.

     


  • Martin_Smith

    My apology, I didn't check this thread for a long time, and i am not quite sure how i can close it. Anyway, I had to change the user interface to accept date format as an user input and surprisingly user prefers this new way to the automated one!
  • How to Read a Date Field from Excel Cell?