Solved

Age calculation

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now