views:

262

answers:

4

I got a binary file, and one of record field stored some date infomation.

The save program is written by VC++ and using the DATE type.

How can read it into C#'s DateTime type ?

A: 

BTW: I tried DateTime.FromBinary, but it looks wrong.

wupher
That's not an answer, you should edit your question instead.
Meta-Knight
A: 

If it's an OLE DATE, use the information available from here:

The DATE type is implemented using an 8-byte floating-point number. Days are represented by whole number increments starting with 30 December 1899, midnight as time zero. Time values are expressed as fractional part. Time precision is 1 second. This type is also used to represent date-only (the fractional part is 0) or time-only (the whole number part is 0) values.

Using this, you can convert to a CLR DateTime.

Vinay Sajip
+1  A: 

As noted in Vinay's answer, the VC++ DATE is really a double. Just read in a double and then convert based on the description.

Here is some sample code for the conversion:

double CDate = 8.50; //The DATE from C++
DateTime newDate = new DateTime(1899, 12, 30);  //Start at 12/30/1899
int days = (int)Math.Floor(CDate); //Get the days (integer part)
double hours = (CDate-days) * 24; //Get the hours (fraction part)

newDate = newDate.AddDays(days).AddHours(hours);

EDIT Or use DateTime.FromOADate as suggested by Joe.

The beauty of SO, you get to learn new things.

Joe Doyle
Thank you, guys. I love here.
wupher
+2  A: 

Try DateTime.FromOADate.

Joe