How to compare the difference between two dates in Delphi?

6

I need to compare 2 dates to see if the expiration date is Bigger than the current date, if it is Minor it returns an error, follows the code I made 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 did I do wrong?

    
asked by anonymous 27.05.2014 / 23:11

3 answers

7

Just a care to take: if you use StrToDate ('20 / 02/2014 ') and the user's machine has an English date pattern, it will give an error.

Whenever you compare dates in the string format, you must invert the formatting. It should be year / month / day. OK?

Code sample:

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;
    
28.05.2014 / 01:22
1

Use the decodeDate() function.

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 YEAR from the date;
  • In the third you pass a variable where the function will extract the MES from the date;
  • In the room you pass a variable to where the function will extract the DAY from the date.

In your case, you call this procedure twice and subtract the value of the two days. More or less like this:

 
    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;
    
29.05.2014 / 02:17
1

As quoted in the Heber answer you can compare dates through operators > and < plus = 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 / times more precisely, at DateUtils .

Examples

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

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 DaysBetween function. :

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;

For the difference of weeks between two dates, use the WeeksBetween function. :

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;

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

Related : Firebird Dates Calculation

    
27.05.2014 / 23:34