Subtract 2 dates into a double

RTaulbee

Active member
Joined
May 4, 2006
Messages
26
Programming Experience
5-10
I'd like to subtract two dates and get a Float/Double instead of an Integer Long as a return value. Is there a date function for this?

Right now I'm using:
VB.NET:
GetMaturityDate = DateDiff(DateInterval.Year, StartDate, EndDate)

But DateDiff returns a Long Integer so it's dropping off the fraction which I want to keep.

Thanks in advance.
 
I should have thought about this more. I guess I could return the days as an integer and divide by 360 to get the double... :eek:
 
The Date data type has got Subtract method that returns value of type TimeSpan. From the TimeSpan you can take the TotalDays property value that return whole and fractional days, which is a Double value.
VB.NET:
Dim diff as Double = EndDate.Subtract(StartDate).TotalDays
 
so now we know.. when we want some fractional number we have to use the resolution of a suitable micro scale to represent the precision required..

if you want your maturity date in years, accurate to the day then you use days.. of course if a maturity date accurate to the month is ok, then you can use months. you can go to the crazy extreme of asking for it in seconds if you need to express something in minutes and fractions of minutes..

do note of course, that working with dates in this manner is often a nuisance, because e.g. years actually have 365.2499 (or something) days but this is manifest as an extra day every 4 years, but not every 400 (or something)

so have a small think about the required precision.. is it going to matter if your date calcs say X number of days, which, if it didnt include leap years, would be 2.99 years, and if it does include leaps is 3.01 - (or something) - does that affect your calc?
 
Back
Top