Hi,
I want to determine the number of days between to dates in dates.
Heres the code I've got The problem I've got is with leap years
Heres the sample data
Earlier Date Later Dater output
29/02/2004 28/02/2005 1.00000 This is the wrong one
28/02/2005 28/02/2006 1.00274
29/02/2004 28/02/2005 1.00274
28/02/2005 28/02/2006 1.00274
What it should return is 1.00274 like the rest.
I want to determine the number of days between to dates in dates.
Heres the code I've got The problem I've got is with leap years
Heres the sample data
Earlier Date Later Dater output
29/02/2004 28/02/2005 1.00000 This is the wrong one
28/02/2005 28/02/2006 1.00274
29/02/2004 28/02/2005 1.00274
28/02/2005 28/02/2006 1.00274
What it should return is 1.00274 like the rest.
Code:
Function ex(LaterDate As Variant, EarlierDate As Variant)
Dim y(1) As Integer
Dim d(1) As Double
Dim years As Integer
Dim daysinyear As Integer
Dim days As Integer
If IsDate(LaterDate) And IsDate(EarlierDate) Then
If EarlierDate <= LaterDate Then
y(0) = Year(EarlierDate)
y(1) = Year(LaterDate)
years = y(1) - y(0)
daysinyear = (DateSerial(y(1) + 1, Month(EarlierDate), Day(EarlierDate)) - DateSerial(y(1), Month(EarlierDate), Day(EarlierDate)))
dt = DateSerial(y(1), Month(EarlierDate), Day(EarlierDate))
If dt > LaterDate Then
dt = DateSerial(y(1) - 1, Month(EarlierDate), Day(EarlierDate))
daysinyear = (DateSerial(y(1), Month(EarlierDate), Day(EarlierDate)) - DateSerial(y(1) - 1, Month(EarlierDate), Day(EarlierDate)))
years = years - 1
End If
days = LaterDate - dt + 1
ex = years + days / daysinyear
Else
ex = 0
End If
Else
ex = CVErr(xlErrNA)
End If
End Function