How to compare the difference between two dates in Delphi?

Asked

Viewed 13,945 times

6

I need to compare 2 dates to know if the due date is Greater that the current date, if it is Minor it returns an error, follows the code I did and did not work.

 var
 Data_Atual: String;
 Data_vencimento: String;
 begin
 Data_Atual := DateToSTr(Date);
 Data_Vencimento := '20/02/2014';
 if (Data_Vencimento < Data_Atual);
 showmessage('A Data de Vencimento é menor que a data atual');
 end;

What I did wrong ?

  • 1

    Use the TYPE DATE.

3 answers

9


Only one precaution to take: if you use Strtodate('20/02/2014') and the user’s machine is default dates in English, it will give error.

Whenever comparing dates in string format, you should invert the formatting. It should stay year / month / day. OK?

Code example:

var
   Data_Atual: string;
   Data_vencimento: string;
 begin
   Data_Atual := FormatDateTime('yyyy/mm/dd', Date);
   Data_Vencimento := '2014/02/20';
   if (Data_Vencimento < Data_Atual) then
     ShowMessage('A Data de Vencimento é menor que a data atual');
 end;
  • worked out perfectly.... thank you!

1

Use the function decodeDate().

In it, you pass 4 parameters:

  • The first is the date;
  • In the second you pass a variable to where the function will extract the date YEAR;
  • In the third you pass a variable to where the function will extract the date MES;
  • In the room you pass a variable to where the function will extract the date DAY.

In your case, you call this procedure twice and subtract the value of the two days. So or so:

    var
    DataInicial, DataFinal : TDateTime;
    AnoInicial, AnoFinal, MesInicial, MesFinal, DiaInicial, DiaFinal : Word;
    DiferencaEntreDias : Integer;

    Begin

      decodeDate(DateFinal, AnoFinal, MesFinal, DiaFinal);
      decodeDate(DataInicial, AnoInicial, MesInicial, DiaInicial);

      DiferencaEntreDias := DiaFinal - DiaInicial;

    end;
  • I’m pretty sure this one coigo{ means Code: and is not part of the programming language, but did not want to change or remove.

  • No, not part... just mistake...

  • So? Oxy! You’re free to [Edit] your posts and improve them... : P

1

As quoted in Heber’s response you can compare dates through the operators > and < besides = to check if they match.

Var
 DataAtual, DataVencimento: String;
Begin
 DataAtual := FormatDateTime('dd/mm/yyyy', Date);
 DataVencimento := '20/02/2014';

 If (DataVencimento = DataAtual) then
   ShowMessage('A Data atual coincide com a Data de Vencimento!')

 Else If (DataVencimento > DataAtual) then
   ShowMessage('A Data Atual é superior a Data de Vencimento!')

 Else If DataVencimento < DataAtual then
   ShowMessage('A Data Atual é inferior a Data de Vencimento!')
End;

If you prefer, there is also a library that provides some functions that allow you to work with dates/hours in a more precise way, the DateUtils.

Examples

To compare the difference between two dates, use the function CompareDate:

Var
 DataAtual, DataVencimento: TDate;
 Value: Integer;
Begin
 DataAtual := StrToDate(FormatDateTime('dd/mm/yyyy', Now));
 DataVencimento := StrToDate('20/02/2014');

 Value := CompareDate(DataAtual, DataVencimento);
 If Value = 0 then
    MessageDlg('A Data Atual coincide com a Data de Vencimento!', mtInformation, [mbOK], 0)
 Else If Value = 1 then
    MessageDlg('A Data Atual é superior a Data de Vencimento!', mtInformation, [mbOK], 0)
 Else
    MessageDlg('A Data Atual é inferior a Data de Vencimento!', mtInformation, [mbOK], 0);
End;

To know the difference of days between two dates use the function DaysBetween:

Var
DataAtual, DataVencimento: TDate;
Diff: String;
Begin
 DataAtual := StrToDate(FormatDateTime('dd/mm/yyyy', Now));
 DataVencimento := StrToDate('20/02/2014');
 Diff := FloatToStr(DaysBetween(DataAtual, DataVencimento));

 MessageDlg(Format('A diferença entre %s e %s é de %s dias', 
       [DateToStr(DataAtual), DateToStr(DataVencimento), Diff]), 
       mtInformation, [mbOK], 0);
End;

To know the difference of weeks between two dates use the function WeeksBetween:

Var
DataAtual, DataVencimento: TDate;
Diff: String;
Begin
 DataAtual := StrToDate(FormatDateTime('dd/mm/yyyy', Now));
 DataVencimento := StrToDate('20/02/2014');
 Diff := FloatToStr(WeeksBetween(DataAtual, DataVencimento));

 MessageDlg(Format('A diferença entre %s e %s é de %s semanas', 
       [DateToStr(DataAtual), DateToStr(DataVencimento), Diff]), 
       mtInformation, [mbOK], 0);
End;

To know the difference of months between dates use the function MonthsBetween, for years use YearsBetween.

Related: Calculation of dates in Firebird

  • even putting Dateutils in USES, when I declared the variable as TDATE, it gave error, saying that TDATE was not defined. Any idea ?

  • when I give a showmessage on current Data_it shows a lot of crazy code, not today’s date...

  • I want to show today’s date, can’t I compare dates using STRING variable for the same dates I did in the above code ? And not using Tdatetime ?

  • if you do so: DATA_ATUAL := Strtodate(Datetostr(Now)); it does not display the date if the variable is TDATETIME as you recommended. Forehead there for you to see.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.