4

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;
Amessihel
  • 5,891
  • 3
  • 16
  • 40
FLASHCODER
  • 1
  • 7
  • 24

1 Answers1

9

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;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Amessihel
  • 5,891
  • 3
  • 16
  • 40
  • 3
    For whatever reason the result is actually a `TValueRelationship`, but effective result is the same. Linking to official [documentation](http://docwiki.embarcadero.com/Libraries/en/System.DateUtils.CompareDate) will reveal such details. – Sertac Akyuz Sep 19 '19 at 22:57
  • @Amessihel, **E2003 Undeclared identifier: 'DateTime'** on: `OldDate := Datetime.Create(2019, 9, 20);` – FLASHCODER Sep 19 '19 at 23:07
  • That create should be TDateTime.Create(...); – Pat Heuvel Sep 19 '19 at 23:10
  • 1
    @PatHeuvel, **'TDateTime' does not contain a member named 'Create'**. – FLASHCODER Sep 19 '19 at 23:13
  • 2
    Note that using `FileDateToDateTime(FileAge(...))` is deprecated, use the overloaded version of `FileAge()` that outputs a `TDateTime` instead: `if FileAge(FileName, UpDate) then ...` – Remy Lebeau Sep 19 '19 at 23:57
  • @BrowJr, oops - looking at the C++ Create! Sorry. – Pat Heuvel Sep 20 '19 at 02:12