INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How to

How can I find the days/months/years between two dates by Rolliee
Posted: 23 Jul 03

Write a global function as shown below:
 
The return value is Years: ??  Months: ?? Days: ?? with the prober values instead of '??'

This handles leap years by using the built in functions of VB.

Public Function DateDiff2(ByVal d1 As Date, ByVal d2 As Date) As String
Dim d3 As Date, nDays As Integer
Dim d1Day As Integer, nDayStart As Integer
Dim nYears As Integer, d1Mon As Integer, MonLast As Integer, nn As Integer, nMon As Integer, d1Year As Integer

    If d1 > d2 Then
      d3 = d1
      d1 = d2
      d2 = d3
    End If

    d1Day = Day(d1)
    d1Mon = Month(d1)
    d1Year = Year(d1)
    nn = 0
    nMon = 0
    nDays = 0
    nYears = 0
  
    MonLast = d1Mon
    Do While d2 > d1
       d1 = d1 + 1
       nn = nn + 1

       nDays = nDays + 1
       
       If (Day(d1) = d1Day) And Month(d1) <> MonLast Then
         nMon = nMon + 1
         nDays = 0
         MonLast = Month(d1)
         If nMon = 12 Then
           nMon = 0
           nYears = nYears + 1
         End If
       End If
    Loop
    

DateDiff2 = "Years: " & Format(nYears, "00") & "   Months: " & Format(nMon, "00") & "   Days: " & Format(nDays, "00")
End Function

Back to Microsoft: Access Forms FAQ Index
Back to Microsoft: Access Forms Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close