I read cell values from excel file that has a several data type (string , dateTime , int) .
And In DateTime cell , that may be in various format likes 12/28/2013 , 8:30 .
Using this code , ( cell.ToString() is excel cell value )
DateTime _dt;
if (DateTime.TryParse(cell.ToString(), out _dt))
{
//
}
When I read 12/28/2013 , it return 12/28/2013 12:00:00 AM .
But when I read 8:30 , it return 12/31/1899 12:00:00 AM .
As you can see , I can't get the original time 8:30 .
Am I wrong something ?
OR is there any approach to fix it ?
You can use
TryParseExactand specify your formats. There is an overload takingstring[]patterns array as a parameter:DateTime.TryParseExactMethod (String,String[],IFormatProvider,DateTimeStyles,DateTime)You can call it like that:
You may need Custom Date and Time Format Strings to prepare more patterns to make sure it works for all your input formats.