Home > database >  Excel DateTime is a string how can I convert to real datetime C#?
Excel DateTime is a string how can I convert to real datetime C#?

Time:09-01

The way my excel is formatted when programming in C# and I pull the cell with the supposed date I get a string "44165". On Excel it shows as a date. I need to convert this to DateTime to do something else. The string is equal to 2021-11-30 when I put it in excel and convert the column to datetime. However when I try to convert to DateTime in C# I get the error "String was not recognized as a valid Datetime".

The value is read using :

cell.CellValue.InnerXml

I have tried

Convert.ToDateTime,
DateTime.Parse,
DateTime.ParseExact 

None of which work and always result in "String was not recognized as a valid Datetime". I cannot change the formatting of the excel I must find a solution within visual studio using C#.

I highly appreciate any help, thanks.

CodePudding user response:

double myOADate = 44165;
DateTime myDate = DateTime.FromOADate(myOADate);

Console.WriteLine(myDate.ToString());

The above returns

30/11/2020 00:00:00

  • Related