wupher
wupher

Reputation: 63

How can I convert VC++'s DATE Type To C#'s DateTime?

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 ?


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

Upvotes: 1

Views: 1954

Answers (3)

Joe Doyle
Joe Doyle

Reputation: 6383

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.

Upvotes: 2

to StackOverflow
to StackOverflow

Reputation: 124696

Try DateTime.FromOADate.

Upvotes: 4

Vinay Sajip
Vinay Sajip

Reputation: 99345

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.

Upvotes: 0

Related Questions