Calculate when you will leave the jail...  

Send By: Alvaro Fernández Conejero
Web : http://www.ddsysnet.com/alvaro
Email: alvaro@ddsysnet.com
Date: 30/01/00

Tip accessed 665 times

 


Do they sentence you to three years, two months and 5 days... when you will leave the jail?

Here you have a function that tells it to you, or what is the same thing, a function that given a date in TDate, calculate another date in TDate, increased or decreased a given number of years, months and days:


 function SumarAFecha(Fecha:TDateTime; Anyos,Meses,Dias:Integer):TDateTime;
 var
   d,m,a:integer;
   d2,m2,a2:word;
 begin
     DecodeDate(Fecha, a2, m2, d2);
     a:=a2+Anyos;
     m:=m2+Meses-1;
     d:=d2+Dias-1;
     if m>0 then
     begin
         a:=a+(m div 12);
         m:=(m mod 12)+1;
     end
     else
     begin
         m:=-m;
         a:=a-(m div 12)-1;
         m:=13-(m mod 12);
     end;
     result:=EncodeDate(a,m,1) + d;
 end;



Call example:

   {Calculo de mi condena por reinventar la rueda:
   3 años, dos meses y 5 dias
   Calcularemos que día salgo de la cárcel...}
   Label1.Caption:=DateToStr( SumarAFecha(Date,3,2,5) );






Other more simply function


Send By: APM@zanzibar.com


 procedure SumarAFecha(var Fecha:TDateTime; Anyos,Meses,Dias:Integer);
    VAR
      m:integer;
  begin
      m:= (Anyos * 12) + Meses;
      Fecha:= IncMonth( Fecha, m ) + dias;
  end;