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.

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
CSWong
Convert.ToDateTime
http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemconvertclasstodatetimetopic.asp
HTH.
Bye
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