Help & Manual authoring tool
How to convert a date of type TFileTime to TDateTime?

Converting from TFileTime to TDateTime

Copyright © 2000 Ernesto De Spirito

Pascal Newsletter. Free ezine for Delphi (and Kylix) programmers with articles, news, reviews, tips, trinks, and links to new Delphi content on the web!

The FindData field of TSearchRec (the record used by FindFirst and FindNext to retrieve directory entries) is another record that among other information (like for example the short and long names of the file) has three fields that represent the creation, last access and last write times (ftCreationTime, ftLastAccessTime, ftLastWriteTime respectively). These three fields are declared as TFileTime, a type that represents 64-bit dates in Coordinated Universal Time (UTC).

If you want to convert these values to TDateTime, you can use the following function:

uses Windows;

function FileTime2DateTime(FileTime: TFileTime): TDateTime;
var
  LocalFileTime: TFileTime;
  SystemTime: TSystemTime;
begin
  FileTimeToLocalFileTime(FileTime, LocalFileTime);
  FileTimeToSystemTime(LocalFileTime, SystemTime);
  Result := SystemTimeToDateTime(SystemTime);
end;

Sample call

procedure TForm1.Button1Click(Sender: TObject);
var
  sr: TSearchRec;
begin
  if FindFirst(Application.ExeName, faAnyFile, sr) = 0 then
  begin
    ShowMessage(DateTimeToStr(
      FileTime2DateTime(sr.FindData.ftLastWriteTime)));
    FindClose(sr);
  end;
end;

You can find the full source code of this article in the archive that accompanies the Pascal Newsletter #21.

JfControls Library - for Delphi and C++ Builder
Copyright © 2000/2006 Ernesto De Spirito.   All rights reserved.