Days to Years

BelfordarrenBelfordarren Member Posts: 8
Hi,

I am trying to work out how many days and years it is until an employee retires.


I have started the code and got it to display the number of days it is until they retire but i am trying to work out a way to convert this to display

For example

Darren 24 years 14 Days

Cheers for you help

Darren :D

Answers

  • Sandeep_PrajapatiSandeep_Prajapati Member Posts: 151
    Hi Darren,
    Here is the function. O:)
    You might need to test it .....!!


    DurationBetween(Date1 : Date;Date2 : Date) : Text[70]
    parameters
    Date1 --- Date
    Date2 --- Date

    local Variables
    month --- Integer
    days --- Integer
    year --- Integer
    DurationText --- Text --- 70
    IF (Date1 > Date2 ) THEN
      ERROR('2nd Date parameter can''t be less than the First one..!!');
    
    year  := DATE2DMY(Date2,3) - DATE2DMY(Date1,3);
    month := DATE2DMY(Date2,2) - DATE2DMY(Date1,2);
    days  := DATE2DMY(Date2,1) - DATE2DMY(Date1,1);
    
    IF days < 0 THEN
    BEGIN 
      CASE month OF 
           1  : days := days + 31;
           2  : CASE (year/4) OF
                     0     : days := days + 29
                     ELSE    days := days + 28;
                END;
           3  : days := days + 31;
           4  : days := days + 30;
           5  : days := days + 31;
           6  : days := days + 30;
           7  : days := days + 31;
           8  : days := days + 31;
           9  : days := days + 30;
           10 : days := days + 31;
           11 : days := days + 30;
           12 : days := days + 31;
      ELSE
      BEGIN
        CASE (month + 12) OF
             1  : days := days + 31;
             2  : CASE ((year - 1)/4) OF
                     0     : days := days + 29
                     ELSE    days := days + 28;
                  END;
             3  : days := days + 31;
             4  : days := days + 30;
             5  : days := days + 31;
             6  : days := days + 30;
             7  : days := days + 31;
             8  : days := days + 31;
             9  : days := days + 30;
             10 : days := days + 31;
             11 : days := days + 30;
             12 : days := days + 31; 
        END;
        year := year - 1;
        month := month + 12;
      END;
      END;
      month := month - 1;
    END;
    
    
    IF month < 0 THEN
    BEGIN
      year := year - 1;
      month := month + 12;
    END;
    
    DurationText := '';
    if (year <> 0) then
    begin
      if (year =  1) then 
        DurationText := DurationText + format(year) + ' Year '
      Else
        DurationText := DurationText + format(year) + ' Years ';
    end;
    
    if (month <> 0) then
    begin
      if (month =  1) then
        DurationText := DurationText + format(month) + ' Month '
      Else
        DurationText := DurationText + format(month) + ' Months ';
    end;
    
    
    if (days <> 0) then
    begin
      if (days =  1) then
        DurationText := DurationText + format(days) + ' Day '
      Else
        DurationText := DurationText + format(days) + ' Days ';
    end;
    Exit(DurationText);
    
    
    I hope it helps .. :P
    Sandeep Prajapati
    Technical Consultant, MS Dynamics NAV
Sign In or Register to comment.