View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Ron Rosenfeld Ron Rosenfeld is offline
external usenet poster
 
Posts: 5,651
Default Convert to/from Excel date

On Thu, 3 Apr 2008 16:54:00 -0700, David Thielen wrote:

Now that's what I call being serious about compatibility :)

I have some C# code that parses an XLSX file. So it reads that the cell is
formatted as a date_time and has a value of 12345.678 - I need to convert
that to year, month, day, hour, second, minute to create a DateTime object.

And then I have to do the reverse when creating an XLSX output file.

One thing I have learned about dates - it is very very difficult to get it
right (Lotus 123 being a good example). So if there is tested code that does
this somewhere, it would be very helpful.

Especially if there are other purposful errors in addition to the 2/29/1900
one.


I don't know about "tested code", but the conversion should be pretty
straightforward, including adding in a day for February 29, 1900. (I have read
that this mistake started with Lotus 1-2-3, which predated Excel, and that MS
incorporated this error into Excel for "compatibility".

Excel stores dates/times as days and fractions of a day. If the worksheet was
using the 1900 date system, then 1 = 1 Jan 1900. And 12 noon on that day would
be represented as 1.5.

If using the 1904 date system, then 1 = 2 Jan 1904 (0 = 1 Jan 1904).

(Date1904 is a Workbook property, but I don't know how that would be stored in
the XLSX file).
--ron