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
Use the TYPE DATE.
– Motta