1. Home
  2. Computing & Technology
  3. Delphi Programming

Convert Milliseconds to Days and Time

By , About.com Guide

When doing lengthy operations in your Delphi applications, you might want to display the time passed to the user.

Here's an example:


var
  startTick, endTick : Int64;
begin
  startTick := GetTickCount;
  SomeLengthyFunction() ;
  endTick := GetTickCount;

  ShowMessage(MSecToTime(endTick - startTick)) ;

The Windows API function GetTickCount retrieves the number of milliseconds that have elapsed since Windows was started.
To get the number of milliseconds that passed while the SomeLengthyFunction was executing you need to subtract endTick value from the startTick value.

Here's a function that takes an amount of milliseconds, converts those milliseconds to days, hours, minutes and seconds and returns the string representation:


function MSecToTime(mSec: Int64): string;
var
  dt : TDateTime;
begin
   dt := mSec / MSecsPerSec / SecsPerDay;
   Result := Format('%d days, %s', ) ;
end;

Note: The MSecsPerSec and SecsPerDay constants are defined in the SysUtils unit.

Delphi tips navigator:
» How to Convert a Bitmap to a Cursor
« How to Parse a Delimited String Into a String List

Explore Delphi Programming
About.com Special Features

Reader's Choice Award Winners

What are the best instant messengers, apps, editors and more? You told us, for our 2010 technology awards program. More >

iPad Central

Is Apple's new tablet computer impractical, a must-have -- or both? We'll help you figure it out. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2005 Delphi Tips
  7. How to Convert an amount of Milliseconds to a TDateTime Value

©2010 About.com, a part of The New York Times Company.

All rights reserved.