Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

Commented Unassigned: Datetime is 1 day off [9497]

$
0
0
Greetings,

I'm trying to write a datetime to a cell using ClosedXML. I set the IXLCell DataType to be XLCellValues.DateTime, and the value to be a new DateTime. I also set the number format id to 14 (mm/dd/yyyy)

When I look at the excel sheet, the date displayed is always one day larger than anticipated. When I read the cell value using ClosedXML (in a unit test), the value read from the file matches the value I wrote, but the actual excel sheet does not show the correct value.

Is this a bug in ClosedXML or am I missing something?

Thanks!
-Adam
Comments: I found the issue. https://msdn.microsoft.com/en-us/library/system.datetime.tooadate(v=vs.110).aspx ToOADate stores dates as days since 30 december 1899, but excel stores them as days since 31 december 1899. Therefore, we need to subtract 1 from the ToOADate value when writing to an excel file, and add 1 to the value when reading from the file.

Viewing all articles
Browse latest Browse all 1877

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>