Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Age calculation

Posted on 2000-03-17
2
Medium Priority
?
422 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 600 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

963 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