Determine number of days between dates

227 views Asked by At

How do I get the whole date difference in VBA?

I know that to get the year or the month or the day I do:

DateDiff("yyyy", Me.DateofBirth, Me.Year).

I want the whole difference. Not just the year, and the two columns that I have are Date of Birth and Year.

1

There are 1 answers

0
Comintern On

Dates are stored internally in VBA as doubles, with the integer portion as the number of days since 1/1/1900. To get the difference between two dates, you can just subtract them:

Dim dob As Date
dob = DateSerial(1990, 1, 1)

Dim difference As Date
difference = Now - dob

Debug.Print Year(difference) - 1900 & " years, " & _
            Month(difference) & " months, " & _
            Day(difference) & " days."

If you want the total number of days, you can just subtract them and use the numeric value of the underlying double:

Dim dob As Date
dob = DateSerial(1990, 1, 1)

Dim days As Long
days = Now - dob
Debug.Print days & " days."