Solved

function for person's age

Posted on 2000-04-17
7
287 Views
Last Modified: 2010-04-16
I need a function that calculates and returns a person's age in years based on his/her birthdate and current date. For example, a person born 5/30/68 would be 21 years old on 5/27/90.  Here's what I have so far:


var month, day, year: integer;
var curmonth, curday, curyr: integer;

function Age (month, day, year: integer
             curmonth, curday, curyr: integer): integer;
       var agemonth: integer;
       var ageday: integer;
       var ageyr: integer;
       var age: integer;

        begin
             agemonth:= curmonth - birmth;
             ageday:= curday - birday;
             ageyr:= curyr - birdyr;


begin {main}
  write ('Enter birth month, day, and year numerically ');
  readln (birmth, birday, birdyr);
  age := Age (month, day, year, curmonth, curday, curyr);
  writeln ('Your age is ', age)


0
Comment
Question by:kazooie21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Author Comment

by:kazooie21
ID: 2724671
Adjusted points from 30 to 50
0
 
LVL 48

Expert Comment

by:dbrunton
ID: 2725496
This is taken from the SWAG archive and I think it is what you need.



{
Allright... I checked around a lot of DATE and TIME routines, and came up
with this, taken from about three different routines. This routine works,
as far as I know, and I've implemented it successfully into my own code.
If anyone knows that this routine has a bug in it, please let me know.

This procedure uses the Julian calander mathmatical equasions to convert
two dates and give the # of days inbetween. If anyone knows a faster way
of writing this procedure, please let me know.
}

type
  string80=string[80];

var
  _retval:integer;

procedure check_date(stream1,stream2:string80);
var
  internal1,internal2:longint;
  JNUM:real;
  cd,month,day,year: integer;
  out:string[25];

    function Jul( mo, da, yr: integer): real;
    var
      i, j, k, j2, ju: real;
    begin
         i := yr;     j := mo;     k := da;
         j2 := int( (j - 14)/12 );
         ju := k - 32075 + int(1461 * ( i + 4800 + j2 ) / 4 );
         ju := ju + int( 367 * (j - 2 - j2 * 12) / 12);
         ju := ju - int(3 * int( (i + 4900 + j2) / 100) / 4);
         Jul := ju;
    end;

begin
  out:=copy(stream1,1,2);
  if copy(out,1,1)='0' then delete(out,1,1);
  val(out,month,cd);
  out:=copy(stream1,4,2);
  if copy(out,1,1)='0' then delete(out,1,1);
  val(out,day,cd);
  out:=copy(stream1,7,2);
  if copy(out,1,1)='0' then delete(out,1,1);
  val(out,year,cd);
  jnum:=jul(month,day,year);
  str(jnum:10:0,out);
  val(out,internal1,cd);
  out:=copy(stream2,1,2);
  if copy(out,1,1)='0' then delete(out,1,1);
  val(out,month,cd);
  out:=copy(stream2,4,2);
  if copy(out,1,1)='0' then delete(out,1,1);
  val(out,day,cd);
  out:=copy(stream2,7,2);
  if copy(out,1,1)='0' then delete(out,1,1);
  val(out,year,cd);
  jnum:=jul(month,day,year);
  str(jnum:10:0,out);
  val(out,internal2,cd);
  _retval:=internal1-internal2;
end;

begin
  check_date('01-01-95','01-01-94');
  writeln('The # of days inbetween is = ',_retval);
end.
0
 
LVL 2

Expert Comment

by:bratt030900
ID: 2731411
you should be able to do something like in the rest of your function:
           
             if ( ageday < 0) then
                  agemonth := agemonth-1;
           
             if (agemonth<0) then
                   ageyr :=ageyr-1;
             
             if (ageyr < 0 ) then
                /* bad date */
                ageyr:=0;
           
             age := ageyr;

/* of course this assumes the dates entered are valid dates*/
0
Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

 
LVL 48

Accepted Solution

by:
dbrunton earned 50 total points
ID: 2735435
program datediff;

var
  year1, month1, day1 : integer;
  year2, month2, day2 : integer;

procedure date_difference(y1, m1, d1, y2, m2, d2 : integer);
var
  yeardiff, monthdiff, daydiff, plus : integer;
  diffok : boolean;
begin
  yeardiff := 0;
  monthdiff := 0;
  daydiff := 0;
  plus := 0;
  diffok := true;
  if d1 >= d2 then
    daydiff := d1 - d2
  else
    begin
      daydiff := 31 - (d2 - d1);
      plus := 1;
    end;
  m2 := m2 + plus;
  plus := 0;
  if m1 >= m2 then
    monthdiff := m1 - m2
  else
    begin
      monthdiff := 12 - (m2 - m1);
      plus := 1;
    end;
  y2 := y2 + plus;
  if y1 >= y2 then
    yeardiff := y1 - y2
  else
    diffok := false;
  if diffok then
    begin
      write('The difference in dates is ', yeardiff, ' year(s) ', monthdiff);
       writeln(' month(s) ', daydiff, ' day(s)');
    end
  else
    writeln('Error.  The second date was more modern - bigger - than the first date');
end;

begin
  writeln('This program expects first date inputted to be newer - bigger - than 2nd');
  writeln('On next 3 lines enter year, month, day of first date');
  readln(year1);
  readln(month1);
  readln(day1);
  writeln('On next 3 lines enter year, month, day of second date');
  readln(year2);
  readln(month2);
  readln(day2);
  date_difference(year1, month1, day1, year2, month2, day2);
end.
0
 

Expert Comment

by:RoelPotman
ID: 2736088
Another suggestion/addition:

Success !

Age = (sysdate - birthdate) / 10000
   

Sysdate = date of today lay-out ccyymmdd for example 20001121 (Nov. 21, 2000)
Birthdate = lay-out ccyyymmdd for example 19921228 (Dec. 28, 1992)

Age=(20001121-19921228) /10000 = 79893 – 10000 = 7,9893 => 7 year
0
 

Expert Comment

by:RoelPotman
ID: 2737445
Sorry, my text was meant as a comment not as THE solution or answer.

Please advise me how to unlock, Roel
0
 

Expert Comment

by:RoelPotman
ID: 2738689
Ì hope this will unlock.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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