Question Datediff or returning fractional differences between 2 dates?

Joined
Aug 29, 2011
Messages
5
Programming Experience
10+
Anyone know how I can return 3.0790 given the two dates below using VB.NET 4?
The code below returns a whole number 3...
Dim datTim1 As Date = #1/31/2005#
Dim datTim2 As Date = #2/29/2008#
Dim YEARS As Double = DateDiff(DateInterval.Year, datTim1, datTim2)
MessageBox.Show("Years: " & YEARS)
 
Private Function GetYearsBetweenDates(date1 As Date, date2 As Date) As Double
    Dim startDate = If(date1 < date2, date1.Date, date2.Date)
    Dim endDate = If(date1 > date2, date1.Date, date2.Date)
    Dim years As Double

    Dim temp1 = startDate
    Dim temp2 = temp1.AddYears(1)

    'Get the whole years.
    Do Until temp2 >= endDate
        years += 1.0
        temp1 = temp2
        temp2 = temp1.AddYears(1)
    Loop

    'Get the partial years.
    years += (endDate - temp1).Days / (temp2 - temp1).Days

    Return years
End Function
 
Private Function GetYearsBetweenDates(date1 As Date, date2 As Date) As Double
Dim startDate = If(date1 < date2, date1.Date, date2.Date)
Dim endDate = If(date1 > date2, date1.Date, date2.Date)
Dim years As Double

Dim temp1 = startDate
Dim temp2 = temp1.AddYears(1)

'Get the whole years.
Do Until temp2 >= endDate
years += 1.0
temp1 = temp2
temp2 = temp1.AddYears(1)
Loop

'Get the partial years.
years += (endDate - temp1).Days / (temp2 - temp1).Days

Return years
End Function


Thanks Jm, that's pretty darn close to what I'm looking for. I'm wondering if it's taking into account the 366 days of the 2008 leap year though. Maybe we could throw in an IsLeapYear...I guess it has to be a subroutine and not datediff, which is one question I had...

Your subroutine returns 3.0792349726776

Here's some psuedocode to illustrate the leap year handling:

InYears:
If year is leap year: (day of year)/366 + year
If not leap year: (day of year)/365 + year

An example will help illustrate this

From 1/31/2005 – 2/29/2008

BeginningDate.InYears() = 2005 + (31/365) = 2005.0849315068493150684931506849
EndingDate.InYears() = 2008 + (60/366) = 2008.1639344262295081967213114754

N = [2008 + (60/366)] – [2005 + (31/365)] = 3.0790029193801931282281607904783 <-- looking for this end-result.


- Red
 
In the partial years expression (endDate - temp1) = 29 days, and (temp2 - temp1) = 366 days with those dates. 3 + 29/366 years seems correct.
 
There must be a slight difference somewhere; otherwise the result numbers would match.

I think I can use this and work with it.

Many thanks JM and John.

- Red
 
I disagree with your interpretation of what constitutes part of a leap year. By your calculation, March 1 2008 to March 1 2009 is not a full year, i.e. (306 / 366) + (59 / 365) = 0.9977. My calculation determines the number of full years and then the fraction by determining the proportion of the next full year the remaining days represents.
 
I disagree with your interpretation of what constitutes part of a leap year. By your calculation, March 1 2008 to March 1 2009 is not a full year, i.e. (306 / 366) + (59 / 365) = 0.9977. My calculation determines the number of full years and then the fraction by determining the proportion of the next full year the remaining days represents.

I should probably clarify. I assume you're referring to my example in the posting above. That's not my interpretation. That is
actually a partial specification for a financial services app. It's an "Annualization" formula that is widely used in the
financial services sector. This is only the "exponent" portion of a larger (vendor supplied) formula. And there is a little bit
more to it, but this is the 'meat-and-potatoes' of the exponent portion... There are some other stipulations regarding
1 full-year's calculation that I intentionally left out.
 
Well, I have shown you the sort of thing you can do by simply adding one year to a date at a time. You can do something similar but modify it to your own needs. You can determine the logic first, which requires no programming experience at all, and then implement that logic in code something like what I did.
 
Back
Top