Solved

Age calculation

Posted on 2000-03-17
2
413 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 43
hibernate jars 4 57
how to debug htl and js pages 8 53
Cisco ASA: Java web start no go, asdm launcher no go 3 57
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 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