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

  • 10 Rules that Make or Break Enterprise App Development Projects In today's app-driven world, application development is a top priority. Even so, 68% of enterprise application delivery projects fail. Designing and building applications that pay for themselves and adapt to future needs is incredibly difficult. Executing one successful project is lucky, but making it a repeatable process and strategic advantage? That's where the money is. With help from our most experienced project leads and software engineers, …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds