FLASHCODER
FLASHCODER

Reputation: 294

How to compare two dates of dd/mm/yyyy format?

I have two files and want compare yours compilation dates for a future update.

Suppose that the new file have a date: 20/09/2019, and old file a date: 19/09/2019. How compare these two date on same format (dd/mm/yyyy)?

var
 UpDate, OldDate: string;
begin
  UpDate := '20/09/2019';
  OldDate := DateToStr(FileDateToDateTime(FileAge(IncludeTrailingBackslash(ExtractFilePath(Application.ExeName)) + 'test.exe'))) // 19/09/2019

  if UpDate > OldDate then
  begin
    // Do something
  end;
end;

Upvotes: 4

Views: 4201

Answers (1)

Amessihel
Amessihel

Reputation: 6394

Instead of manipulating strings, you can deal directly with TDateTime values by invoking DateUtils.CompareDate().

var  OldDate, UpDate : TDateTime;
begin
  OldDate := EncodeDate(2019, 9, 20);
  UpDate := FileDateToDateTime(FileAge(IncludeTrailingBackslash(ExtractFilePath(Application.ExeName)) + 'test.exe'));
  if CompareDate(OldDate, UpDate) = LessThanValue  Then
  begin
    // Do something
  end;
end;

Upvotes: 9

Related Questions