Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

Console.WriteLine(myDate.ToString());

The above returns

30/11/2020 00:00:00

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading