# Calculating the Years Between Dates

### WEBINAR:On-Demand

Application Security Testing: An Integral Part of DevOps

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
```

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.

#### 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.

• #### 11hvariava

Posted by Humairaa on 11/18/2014 02:08am

this does not work

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

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

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

• You must have javascript enabled in order to post comments.

## Top White Papers and Webcasts

• As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
• The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
• Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...