The following code returns the age from a particular database.
What do I add to get the exact date in years, months, days?
<%= DateDiff("yyyy",rs("Dateofbirth"),date)%>
i.e. result should be 12 yrs 6 months 8 days old.
The following code returns the age from a particular database.
What do I add to get the exact date in years, months, days?
<%= DateDiff("yyyy",rs("Dateofbirth"),date)%>
i.e. result should be 12 yrs 6 months 8 days old.
DateDiff("d",rs("Dateofbirth"),date) will give you a number of days. DateDiff("m",rs("Dateofbirth"),date) will give you a number of months.
So, something like(I don't know exactly what is rs()) :
CurrentDate = rs("Dateofbirth")
Years = DateDiff("yyyy", CurrentDate ,date)
ThisYear = DateAdd("yyyy", Years, CurrentDate)
Months = DateDiff("m", ThisYear ,date)
ThisMonth = DateAdd("m", Months, CurrentDate)
Days = DateDiff("d", ThisMonth, date)
Age = CStr(Years) & " years" & CStr(Months) & " months" & CStr(Days) & Days
But after some toying, it didn't always work. The difference is somewhat rounded up. So I had negative numbers for days or months, in some occurences. So I went mad and overprotected the code :
MsgBox(Age("09-12-1946"))
Function Age(DateOfBirth)
Dim CurrentDate, Years, ThisYear, Months, ThisMonth, Days
CurrentDate = CDate(DateOfBirth)
Years = DateDiff("yyyy", CurrentDate, Date)
ThisYear = DateAdd("yyyy", Years, CurrentDate)
Months = DateDiff("m", ThisYear, Date)
ThisMonth = DateAdd("m", Months, ThisYear)
Days = DateDiff("d", ThisMonth, Date)
Do While (Days < 0) Or (Months < 0)
If Days < 0 Then
Months = Months - 1
ThisMonth = DateAdd("m", Months, ThisYear)
Days = DateDiff("d", ThisMonth, Date)
End If
If Months < 0 Then
Years = Years - 1
ThisYear = DateAdd("yyyy", Years, CurrentDate)
Months = DateDiff("m", ThisYear, Date)
ThisMonth = DateAdd("m", Months, ThisYear)
Days = DateDiff("d", ThisMonth, Date)
End If
Loop
Age = Years & "y/" & Months & "m/" & Days
End Function
It's probably excessively defensive code(with many repeated lines, bad bad bad), but it works. I leave you making it prettier. Or ask on Code Review for a nicer code.
Perhaps the quickest and simplest way is to divide the date difference by 365.25, like so:
Dim age, dob
dob = CDate("01-Jan-1980")
age = (Now() - dob) / 365.2425
Though it's not exactly accurate, it's most likely accurate enough for 99 .99% of usage cases (i.e. where you don't need to calculate to the exact second).
Based on Age calculations in the other questions come up with this for a more accurate age check.
Dim Age, CurrentDate, BirthDate
CurrentDate = Now()
BirthDate = CDate(rs("Dateofbirth") & "")
'Convert to yyymmdd
CurrentDate = Year(CurrentDate) & Right("00" & Month(CurrentDate), 2) & Right("00" & Day(CurrentDate), 2)
BirthDate = Year(BirthDate ) & Right("00" & Month(BirthDate ), 2) & Right("00" & Day(BirthDate ), 2)
Age = Fix((0 + CurrentDate - BirthDate) / 10000)
Response.Write Age
Based on @dotjoe's answer from How to calculate age (in years) based on Date of Birth and getDate()
this the final answer for the specific code
function getAge(DateOfBirth)
'get age in years
ageyears = DateDiff("yyyy",DateOfBirth,date())
'get month of dob
BirthMonth = Month(DateOfBirth)
'get todays month
CurrMonth = Month(date)
'get day of dob
ageDay = Day(Date)
If CurrMonth< BirthMonth Then
Agemonth= 12 + CurrMonth - BirthMonth
else
agemonth = CurrMonth - BirthMonth
end if
Age= ageyears&"years "&ageMonth&" months "&ageDay& "days"
getAge =Age
end function
Thanks all for your help
Try the below code:
' Usage:
' GetDiff(date1, date2, accuracy)
' date1 - first date
' date2 - second date
' accuracy - output format: from 1 (N years) to 6 (N years, N months, N days, N hours, N minutes, N seconds)
MsgBox GetDiff(CDate("09-11-2014 10:55:30"), CDate("08-10-2016 15:45:10"), 6)
Function GetDiff(d1, d2, a)
Dim r
ReDim r(a - 1)
Delta 0, Array("yyyy", "m", "d", "h", "n", "s"), Array("years", "months", "days", "hours", "minutes", "seconds"), r, d1, d2, False
GetDiff = Join(r, ", ")
End Function
Sub Delta(i, t, n, r, d1, d2, c)
Dim q, d
q = DateDiff(t(i), d1, d2)
If UBound(r) > i Then
Do
d = DateAdd(t(i), q, d1)
Delta i + 1, t, n, r, d, d2, c
If c Then Exit Do
q = q - 1
Loop
End If
c = q >= 0
r(i) = q & " " & n(i)
End Sub