简体   繁体   中英

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

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.

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

Console.WriteLine(myDate.ToString());

The above returns

30/11/2020 00:00:00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM