Solved

Age calculation

Posted on 2000-03-17
2
411 Views
Last Modified: 2012-05-04
Hi,
I have a method that calculates a person's current age in years (which I now have rewritten). Now I'd like to calculate the age in years, months and days as well.
On the web I found a Javascript method that did this, but I found that it didn't calculate correctly every time.
The code looks like this after I've ported it to Delphi:

procedure CalculateAge(var nYears, nMonths, nDays: Integer; const dtBirthday, dtToday: TDateTime);
var
  yyBd, mmBd, ddBd, yyTd, mmTd, ddTd: Word;
  nY, nM, nD: Integer;
begin
  DecodeDate(dtToday, yyTd, mmTd, ddTd);
  DecodeDate(dtBirthday, yyBd, mmBd, ddBd);

  nY := yyTd - yyBd;

  if (mmTd >= mmBd) then
    nM := mmTd - mmBd
  else
    begin
      Dec(nY);
      nM := 12 + mmTd - mmBd;
    end;

  if (ddTd >= ddBd) then
    nD := ddTd - ddBd
  else
    begin
      Dec(nM);
      nD := 31 + ddTd - ddBd;

      if (nM < 0) then
        begin
          nM := 11;
          Dec(nY);
        end;
    end;
  nYears := nY;
  nMonths := nM;
  nDays := nD;
end;

Could someone please tell me what is wrong with the code snipet above? Does anyone have a better solution? I'm completely stuck. I've tried almost everything.

Hope someone could help me...

-Jorgen
0
Comment
Question by:jorged
2 Comments
 
LVL 2

Accepted Solution

by:
stalefish earned 200 total points
ID: 2638793
I don't know Delphi, but here is a simple answer that you can use. Basically the first thing I do is convert the birthday and the current date  in to days, and subtract the two. From that value you get all your answers. This doesn't take into account leap years. You could easily include this by either dividing by 365.25 (approximate answer) or by determining the number of leap days between now and then adding it to the total (tYear-days % 4 (modulos))

class AgeCalculator {
  public final static int[] DAYSINMONTH = {12,28,31,30,31,30,31,31,30,31,30,31};
      
  public static void main(String[] args){
    CalculateAge(21, 5, 75, 20, 3, 100);
  }
      
public static void CalculateAge(int day, int month, int year, int tDay, int tMonth, int tYear){
  int then = year*365;
  for(int i = 0; i<month; i++) then += DAYSINMONTH[i];
  then += day;
            
  int today = tYear*365;
  for(int i = 0; i<tMonth; i++) today += DAYSINMONTH[i];
  today += tDay;
            
  double daysOld = today - then;
  double monthsOld = daysOld/12;
  double yearsOld = daysOld/365;
  System.out.println("Days Old = " + daysOld);
  System.out.println("Months Old = " + monthsOld);
  System.out.println("Years Old = " + yearsOld);
      
}

}
0
 

Author Comment

by:jorged
ID: 2639108
Thank you,
I ported your code to Delphi and it works fine, but the code I've written myself while you wrote me this code, looks like it's a similar solution.

I also seem to have posted this question on the wrong forum, so this won't happen again. Sorry.

-Jorgen
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
more than one jdk and one jre 1 41
Way to decrease size of apk file 9 65
eclipse apache tomcat admin console 52 96
Android development question 2 34
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question