473,508 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using DATEDIFF to calculate age in years, months, and days

6 New Member
I know it's been asked and answered, but I'm still not following. I'm an accountant using MS Access to create an Human Resources database. I am looking for the DATEDIFF formula to calculate the age of a person in Years, Months, and Days. I've copied and pasted formulas I've found on the net and am NOT getting the right results, though the format (Y-M-D) is correct. For example, I am 46 years, 5 months, and 1 day old, yet the answer I get is 47 years, 7 months, and 199 days.

Here is the formula:

=DateDiff("yyyy",[Birth Date],Date()) & " yr(s). " & DateDiff("m",[Birth Date],Now())-(DateDiff("yyyy",[Birth Date],Now())*12) & " mo(s)." & DateDiff("d",[Birth Date],Now())-(DateDiff("yyyy",[Birth Date],Now())*365) & " day(s)"

Any help (like actually writing the formula so I can copy and paste) would be greatly appreciated. Thanks.
Feb 16 '17 #1
7 15895
gnawoncents
214 New Member
The problem is parsing out the individual fields. You need to do one DateDiff for the whole, then get the parts. I imagine the 7 months and 199 days might include a negative sign on one or both (indicating it is 47 years, minus 7 months and/or 199 days. This would happen if you parse out months and get January(1) - August(8) = 7.
Feb 16 '17 #2
GLK913
6 New Member
You are correct... 47 yrs. -7 mos. -199 days.
Feb 16 '17 #3
gnawoncents
214 New Member
No result is going to be exact, since Access doesn't have a built-in feature for this, but the code below should get you within +/- 1 day.

Expand|Select|Wrap|Line Numbers
  1. Dim dblYears As Double
  2. Dim intYears As Integer
  3. Dim dblMonths As Double
  4. Dim intMonths As Integer
  5. Dim intDays As Integer
  6.  
  7. intDays = DateDiff("d", Me.[Birth_Date], Date)
  8. dblYears = intDays / 365.25
  9. intYears = Fix(dblYears)
  10. dblMonths = (dblYears - intYears) * 12
  11. intMonths = Fix(dblMonths)
  12. intDays = ((dblMonths - intMonths) / 12) * 365
  13.  
  14. Me.txtResult = intYears & " years, " & intMonths & " months, " & intDays & " days"
  15.  
Feb 16 '17 #4
jforbes
1,107 Recognized Expert Top Contributor
You also might find these useful:

Age() Function

Expand|Select|Wrap|Line Numbers
  1. Public Function getAge(ByVal vDateOfBirth As Variant, ByVal vDateReference As Variant) As String
  2.  
  3.     Dim iDays As Integer
  4.     Dim iMonths As Integer
  5.     Dim iYears As Integer
  6.     Dim iDaysInMonth As Integer
  7.     Dim dDateOfBirth As Date
  8.     Dim dDateReference As Date
  9.  
  10.     dDateOfBirth = Int(Nz(vDateOfBirth, Now()))
  11.     dDateReference = Int(Nz(vDateReference, Now()))
  12.  
  13.     iDaysInMonth = DateSerial(Year(dDateOfBirth), Month(dDateOfBirth) + 1, 1) - DateSerial(Year(dDateOfBirth), Month(dDateOfBirth), 1)
  14.  
  15.     iYears = DatePart("yyyy", dDateReference) - DatePart("yyyy", dDateOfBirth)
  16.     iMonths = DatePart("m", dDateReference) - DatePart("m", dDateOfBirth)
  17.     iDays = DatePart("d", dDateReference) - DatePart("d", dDateOfBirth)
  18.     If iDays < 0 Then
  19.         iMonths = iMonths - 1
  20.         iDays = iDaysInMonth + iDays
  21.     End If
  22.     If iMonths < 0 Then
  23.         iYears = iYears - 1
  24.         iMonths = 12 + iMonths
  25.     End If
  26.  
  27.     getAge = iYears & " yr(s).  " & iMonths & " mo(s).  " & iDays & " day(s)"
  28. End Function
Feb 16 '17 #5
GLK913
6 New Member
I'm assuming the above is VBA code. If so, where do I copy and paste that? Not sure how this all works. Thanks.
Feb 16 '17 #6
gnawoncents
214 New Member
You would add the jforbes code to your VBA script on the applicable form, then call it using the getAge([Birth_Date]) command.

My code could be added to VBA using an event (button on_click or a field after_update, for example.
Feb 16 '17 #7
GLK913
6 New Member
It worked! Thanks so much for your help.
Feb 17 '17 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
27935
by: carl.barrett | last post by:
Hi, I have a continuous form based on a query ( I will also be creating a report based on the same query). There are 2 fields: Date Obtained and Date Of Expiry I want a further 3 columns...
5
8805
by: Juan | last post by:
Hi everyone, is there a function that calculates the exact amount of Years, Months, and Days between two days? TimeDiff is not good enough, since it calculates, for example: Date 1: Dec....
7
15468
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
8
1757
by: Jose | last post by:
Exists a function that determined between two dates the years,months and days? Thanks a lot.
3
5943
by: Libber39 | last post by:
Hi everyone, Have a query on how to calculate the amount of weeks and days contained in a number in an access query. ie: the difference in days between 2 dates amounts to 17 days. I want to now...
15
42594
karthickkuchanur
by: karthickkuchanur | last post by:
Hai sir, i assigned to calculate the age from current date ,i have the date of birth of employee from that i have to calculate the age dynamically, i refer to google it was confusing...
2
3999
by: johanneguaybenoit | last post by:
Hi I would like to know how to create a function or module to calculate the age of a person in years months days. and hours if feasable But I really nead to know in years months days. I...
2
3565
by: Louise Harrison | last post by:
I had read a previous article on calculating the number of years, months and days between 2 dates. They had used the DateDiff with a start date and an end date, similiar like below statement: ...
2
1913
by: wilson52 | last post by:
I'm trying to update a form field to calculate number of days overdue based on another projected training date field on my form. This is what I have on my control source line: ...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.