Calculating the Years Between Dates

Business applications often find it useful to calculate the number of years between two particular dates, such as the date a customer first ordered and the present date, perhaps to see whether they apply for a "loyalty" discount or a free gift.

Don't scramble in the code window. Just use my next little snippet. Simply call YearsBetweenDates, passing in a start date and end date. It'll return an Integer containing the number of full years between the specified dates:

Public Function YearsBetweenDates(ByVal StartDate As DateTime, _
       ByVal EndDate As DateTime) As Integer
       ' Returns the number of years between the passed dates
       If Month(EndDate) < Month(StartDate) Or _
          (Month(EndDate) = Month(StartDate) And _
          (EndDate.Day) < (StartDate.Day)) Then
           Return Year(EndDate) - Year(StartDate) - 1
   Else
           Return Year(EndDate) - Year(StartDate)
   End If
End Function

About the Author

Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2, $49.99), plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.



About the Author

Karl Moore

Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is the author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2), plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.

Comments

  • Add on . . .

    Posted by WzBn on 09/14/2005 12:56pm

    the following routine give extras info about years, days and months: Private Function DateToString(ByVal dtLower As Date, ByVal dtHigher As Date) As String Dim sRes As New System.Text.StringBuilder Try Dim lyears, lmonths, ldays As Long lyears = dtHigher.Year - dtLower.Year If dtHigher.Month = dtLower.Month Then ' Mes Igual lmonths = 0 If dtHigher.Day = dtLower.Day Then ' Dia Igual ldays = 0 ElseIf dtHigher.Day > dtLower.Day Then 'Dia Menor ldays = dtLower.Day - dtHigher.Day Else 'Dia Mayor lyears = Convert.ToInt32(IIf(lyears > 0, lyears - 1, 0)) lmonths = 11 ldays = (DaysperMonths(dtLower) - dtLower.Day) + dtHigher.Day End If ElseIf dtHigher.Month > dtLower.Month Then ' Mes Menor lmonths = dtHigher.Month - dtLower.Month If dtHigher.Day = dtLower.Day Then ' Dia Igual ldays = 0 ElseIf dtHigher.Day > dtLower.Day Then 'Dia Menor ldays = dtHigher.Day - dtLower.Day Else 'Dia Mayor lmonths -= 1 ldays = (DaysperMonths(dtLower) - dtLower.Day) + dtHigher.Day End If Else ' Mes Mayor lyears = Convert.ToInt32(IIf(lyears > 0, lyears - 1, 0)) lmonths = dtHigher.Month + (12 - dtLower.Month) If dtHigher.Day = dtLower.Day Then ' Dia Igual ldays = 0 ElseIf dtHigher.Day > dtLower.Day Then 'Dia Menor 'lmonths -= 1 ldays = dtHigher.Day - dtLower.Day Else 'Dia Mayor lmonths -= 1 ldays = (DaysperMonths(dtLower) - dtLower.Day) + dtHigher.Day End If End If sRes.AppendFormat("{0},{1},{2}", Math.Abs(lyears), Math.Abs(lmonths), Math.Abs(ldays)) Catch ex As Exception 'Debug.WriteLine(ex.ToString) End Try Return (sRes.ToString) End Functio

    Reply
  • what about using

    Posted by calin on 07/27/2004 03:40pm

    DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Wednesday, September 24, 2014 8:00 AM - 9:00 AM PDT According to a recent Forrester Research report, many companies are choosing low-code platforms over traditional programming platforms, due to the speed with which low-code apps can be assembled and tested. With customer-facing applications on the rise, traditional programming platforms simply can't keep up with the "short schedules and rapid change cycles" required to develop these applications. Check out this upcoming webinar and join Clay Richardson from …

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds