View Single Post
Old 06-29-2009   #10 (permalink)
Todd Vargo


 
 

Re: Date duration function that returns years, months, weeks and days?

Pegasus [MVP] wrote:
Quote:

>
> "HAL07" <yahoohal@xxxxxx> wrote in message
> news:%23fAtxHL%23JHA.4648@xxxxxx
>
Quote:

> > thanks for helping me write one, however I asked if anybody had such a
> > function ready.
> > --
> > -- HAL07, Engineering Services, Norway
>
> You might have to do some of the work yourself. It's quite basic. Maybe
some
Quote:

> respondent will agree to spend his own time and deliver it to you on a
> platter but he/she would, of course, need a tight specification about the
> ambiguous "month" requirement.
I wrote this code some time ago. By providing a DOD (date of death), it
tells how many ymd a person lived. I leave the hours, minutes and seconds as
an exercise for OP to work out. HTH.

'GetAge_in_YMDs.vbs
'Displays number of years, months and days elapsed since a specified date.

On Error Resume Next
DOB = CDate(InputBox("Enter your Birth date:", "Birthday calculator"))
If Err.Number > 0 Then Wscript.Quit
On Error Goto 0

'Displays the number of years, months and days between two dates by
'adding a second InputBox for DOD, default is todays date.
DOD = Date

Wscript.Echo GetAge(DOB, DOD)



Function GetAge(DOB, DOD)

'Ensure the DOB is LESS than DOD
If DOB > DOD Then
MsgBox "Begin date is greater than end date.", _
vbOKOnly + vbInformation, "Unacceptable Date"
Wscript.Quit
End If

'Get the years between the two dates
yrs = DateDiff("yyyy", DOB, DOD)
yrs = yrs - Abs(DateAdd("yyyy", yrs, DOB) > DOD)

'Get the months between the two dates that exceed the years
mos = DateDiff("m", DOB, DOD)
mos = mos - Abs(DateAdd("m", mos, DOB) > DOD) - (yrs * 12)

'Get the number of days between the two dates that exceed the years +
months ...
dys = DateDiff("n", DateAdd("m", mos + yrs * 12, DOB), DOD) \ 1440

'Build strings
If yrs = 1 Then
yrs = yrs & " year, "
Else
yrs = yrs & " years, "
End If

If mos = 1 Then
mos = mos & " month, "
Else
mos = mos & " months, "
End If

If dys = 1 Then
dys = dys & " day."
Else
dys = dys & " days."
End If

'Return the string
GetAge = yrs & mos & dys

End Function


--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

My System SpecsSystem Spec