Calculating years, months and days between two dates

dbmanalser84

Registered User.
Local time
Today, 01:15
Joined
Feb 26, 2008
Messages
52
I have a form where I enter employees and the dates they've started working.

I used the following code and managed to display the days passed from the start date up until now:

Code:
Dim pocetakRada As Date
Dim trajanjeOdnosa As Integer

pocetakRada = [dtePocetakRadnogOdnosa].Value

trajanjeOdnosa = DateDiff("d", pocetakRada, Now())

[Vrednost1].Value = trajanjeOdnosa

[dteRadniOdnosUUstanovi].Value = trajanjeOdnosa

What I want is to display not only the days passed but years months and days also. The data is to be displayed in a text field.

How do I do it?

Thnx.
 
I think that you can find samples in the code repository of the reference part of the forum.

Brian
 
Hi -

Try this:

Code:
Function fAgeYMD(startdate As Date, EndDate As Date) As String
'Purpose:   Returns the difference between StartDate and EndDate in full years, months and days
'Coded by:  raskew
'To call:
' ? fAgeYMD(#7/6/54#, #10/3/84#)
'Returns:
' 30 years 2 months 28 days

Dim intHold As Integer
Dim dayHold As Integer

   intHold = Int(DateDiff("m", startdate, EndDate)) + _
             (EndDate < DateSerial(year(EndDate), month(EndDate), day(startdate)))

   If day(EndDate) < day(startdate) Then
      dayHold = DateDiff("d", startdate, DateSerial(year(startdate), month(startdate) + 1, 0)) + day(EndDate)
   Else
      dayHold = day(EndDate) - day(startdate)
   End If
   
   fAgeYMD = Int(intHold / 12) & " year" & IIf(Int(intHold / 12) <> 1, "s ", " ") _
             & intHold Mod 12 & " month" & IIf(intHold Mod 12 <> 1, "s ", " ") _
             & LTrim(str(dayHold)) & " day" & IIf(dayHold <> 1, "s", "")

End Function

HTH - Bob
 
Bob

yours is one of the samples in the code repository, I don't invent the wheel if somebody cleverer has already done it. :D

Brian
 
Thanks Brian -

How should I spell redundant?

I'd forget I'd put in the Code Repository (a couple of years ago) till you pointed it out.

Old age seems to be starting to take its toll.

Best wishes - Bob
 

Users who are viewing this thread

Back
Top Bottom