How to compare difference between two dates in Delphi?

Question:

I need to compare 2 dates to know if the due date is greater than the current date, if it is less it returns an error, follows the code I made and it didn't 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 ?

Answer:

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

Whenever comparing dates in string format, you must reverse the formatting. Must 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;
Scroll to Top