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

  • Instead of only managing projects organizations do need to manage value! "Doing the right things" and "doing things right" are the essential ingredients for successful software and systems delivery. Unfortunately, with distributed delivery spanning multiple disciplines, geographies and time zones, many organizations struggle with teams working in silos, broken lines of communication, lack of collaboration, inadequate traceability, and poor project visibility. This often results in organizations "doing the …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds