Diferent in year dates

gonzy1981gonzy1981 Member Posts: 156
If i've two dates and i want to calculate the different in years, do you know to do it??
Thanks

Comments

  • kapamaroukapamarou Member Posts: 1,152
    Can you give an example with the required result?
  • gonzy1981gonzy1981 Member Posts: 156
    I have to calculate the age of one person, i have two dates:
    - First date, the date that person was born
    - Secund date, Workdate.
    And i want to know if that person has more of 16 years.
  • kapamaroukapamarou Member Posts: 1,152
    You could use a Duration datatype to be more accurate or you could Subtract dateA from dateB to get the difference in days and the divide by the days per year... Something like that...
  • gonzy1981gonzy1981 Member Posts: 156
    I'm going to try with duration.
    But the problem with the other option is that, some years have less/more days then others and if i divide by 365 the age is not exacty.

    I think that there is a Nav's rutine that do it, but a I don't know
  • gonzy1981gonzy1981 Member Posts: 156
    With duration is not possible.

    Do you know, how to calculate the days into two dates?? i'll try to do it dividing into 365, because i don't find any rutine that give me in years diferent in dates.

    Because i subtract two dates, but the subtract return me in a datatype in integer (Nav can't put a datatype 'date' or 'datetime') that has a value wrong
  • kapamaroukapamarou Member Posts: 1,152
    Although it could work and you could look for leap years then try a different approach.

    Let's say:
    BithDate := SomeDate;
    birthDate16 := CALCDATE('+16Y',BithDate);
    IF birthDate16 = TODAY THEN 
      MESSAGE("The Party Is On");
    IF birthDate > TODAY THEN 
      MESSAGE("You must buy a present");
    IF birthDate16 < TODAY THEN 
      MESSAGE("Nice Memory...");
    
  • AlbertvhAlbertvh Member Posts: 516
    Hi

    maybe for something simple you could do
    Years := DATE2DMY(TODAY,3) - DATE2DMY(BirthDate,3);
    

    I recall seeing something about this in the Tips & Tricks

    Hope this helps.

    Albert
  • kapamaroukapamarou Member Posts: 1,152
    Albertvh wrote:
    Hi

    maybe for something simple you could do
    Years := DATE2DMY(TODAY,3) - DATE2DMY(BirthDate,3);
    

    I recall seeing something about this in the Tips & Tricks

    Hope this helps.

    Albert


    This way you subtract years. If the birthday is on December and TODAY is on January then the age will be wrong. It will give for example 16, but the actual age will be 15 and a month...
  • AlbertvhAlbertvh Member Posts: 516
    Hi
    As I said
    maybe for something simple you could do :D

    I found this link

    Calculate Birthday - Age and Days


    Edit: This is in the Tips & Tricks (URL didn't work)
  • David_SingletonDavid_Singleton Member Posts: 5,479
    edited 2008-11-17
    KISS please.
    if  calcdate('<CD+16Y>',BirthDate) < today then 
      message('Older than Sixteen');
    

    Not tested, so you may need to tweak it, but you should be able to fix it from there.

    8><--- Should be "<" not greater.
    David Singleton
  • David_SingletonDavid_Singleton Member Posts: 5,479
    kapamarou wrote:
    Let's say:
    BithDate := SomeDate;
    birthDate16 := CALCDATE('+16Y',BithDate);
    ....
    

    :D Opps I see you already suggested the same thing.
    David Singleton
  • ErictPErictP Member Posts: 164
    Try this function.
    CalcAge("Birth Date",WORKDATE);
        PROCEDURE CalcAge@1000000013(pStartDate@1000000000 : Date;pEndDate@1000000005 : Date) iYear : Integer;
        VAR
          YearNumber@1000000001 : Integer;
          BirthDay@1000000002 : Date;
          CompareDate@1000000003 : Date;
          ReturnNumber@1000000004 : Integer;
        BEGIN
          IF (pStartDate = 0D) OR (pEndDate = 0D) THEN EXIT(0);
          BirthDay := pStartDate;
          YearNumber := DATE2DMY(BirthDay,3);
          ReturnNumber := DATE2DMY(pEndDate,3) - YearNumber;
    
          IF (DATE2DMY(BirthDay,1) = 29) AND  (DATE2DMY(BirthDay,2) = 2) THEN
            IF NOT ((DATE2DMY(pEndDate,1) = 29) AND  (DATE2DMY(pEndDate,2) = 2)) THEN
              BirthDay := BirthDay -1;
          
          CompareDate := DMY2DATE(DATE2DMY(BirthDay,1),DATE2DMY(BirthDay,2),DATE2DMY(pEndDate,3));
          IF (CompareDate - pEndDate) > 0 THEN
            ReturnNumber := ReturnNumber -1;
    
          iYear := ReturnNumber;
        END;
    
    
Sign In or Register to comment.