Calculating dates in firebird

4

I am creating a report to demonstrate the period of dismissal of employees, so I need to calculate the days, months and years that it has been removed, and I am having a problem, in Firebird I use something like datediff(day,dtini,dtfim) , the problem is that if passed a period as 02/02/2014 and 02/02/2014 says that there were 0 days of removal, one problem is that datediff returns me one day less.

Another problem is that I do not know a function in Firebird that returns periods in days, months and years, already considering the number of days of each month, leaving a margin of error in the calculation since I'm doing this calculation in an approximate way.

    
asked by anonymous 03.12.2014 / 12:36

1 answer

3

Delphi does not have a native function that returns the humanized date difference, as @Omni said in the chat, but you can use delphi's DateUtils to generate this information:

Note: In your case as a day to itself is a day of removal, you must add 1 to the end date

function TForm1.PeriodoExtenso(const DataInicial, DataFinal: TDate): string;
var
  Anos, Meses, Dias: Integer;
  DataAjustada: TDateTime;
  Periodo: string;
begin
  Periodo := EmptyStr; //Variaveis locais no delphi não são inicializadas por padrao

  Anos := YearsBetween(DataInicial, DataFinal);
  DataAjustada := IncYear(DataInicial, Anos);
  Meses := MonthsBetween(DataAjustada, DataFinal);
  DataAjustada := IncMonth(DataAjustada, Meses);
  Dias := DaysBetween(DataAjustada, DataFinal);

  if Anos > 0 then
    Periodo := Format('%d anos', [Anos]);

  if Meses > 0 then
  begin
    if Periodo <> EmptyStr then
      Periodo := Periodo + ' e ';

    Periodo := Format('%s%d meses', [Periodo, Meses]);
  end;

  if Dias > 0 then
  begin
    if Periodo <> EmptyStr then
      Periodo := Periodo + ' e ';

    Periodo := Format('%s%d dias', [Periodo, dias]);
  end;

  Result := Periodo;
end;
    
03.12.2014 / 15:21