[AccessD] Dividing days into years and months

Douglas Barnes doug at starntech.com
Wed Jul 28 07:50:48 CDT 2004


Try looking at the DATESERIAL function

Function Age(Date1 As Date, Date2 As Date) As String
    Dim Y As Integer
    Dim M As Integer
    Dim D As Integer
    Dim Temp1 As Date
    Temp1 = DateSerial(Year(Date2), Month(Date1), Day(Date1))
    Y = Year(Date2) - Year(Date1) + (Temp1 > Date2)
    M = Month(Date2) - Month(Date1) - (12 * (Temp1 > Date2))
    D = Day(Date2) - Day(Date1)
    If D < 0 Then
        M = M - 1
        D = Day(DateSerial(Year(date2), Month(date2), 0)) + D
    End If
    Age = Y & " years " & M & " months " & D & " days"
End Function



Douglas Barnes
Starn Technical Services
P. O. 1172
Meadville, PA 16335

814.724.1045

doug at starntech.com
www.starntech.com

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com]On Behalf Of Scott Marcus
Sent: July 28, 2004 8:10 AM
To: Access Developers discussion and problem solving
Subject: RE: [AccessD] Dividing days into years and months


Here is a function that is approximate. You will need to add code for leap
years...

Public Function YMD(ByVal startdate As Date, ByVal enddate As Date)
Dim intYears As Integer
Dim intMonths As Integer
Dim intDays As Integer

intDays = DateDiff("d", startdate, enddate)
intYears = intDays / 365
intMonths = (intDays / 365) * 12 - (intYears * 12)
intDays = intDays - (intYears * 365) - ((intMonths / 12) * 365)
YMD = "Years:" & intYears & " Months:" & intMonths & " Days:" & intDays
End Function

--
_______________________________________________
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com




More information about the AccessD mailing list