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

  • 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 …

  • Live Webinar Tuesday, August 26, 2014 1:00 PM EDT Customers are more empowered and connected than ever, and the customer's journey has grown more complex. Their expectations are growing and trust is diminishing as they may interact with multiple brands through web, mobile and social channels. Considering 70% of the buying process in a complex sale is already complete before prospects are willing to engage with a live salesperson -- it's critical to understand your customers and anticipate their needs.* …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds