我有个现成的vcl,现奉献出来:
{*************************************************************}
{ DateTime Calculator component for Delphi 16/32 }
{ Version: 1.3 }
{ Author: Aleksey Kuznetsov }
{ E-Mail: info@utilmind.com }
{ Home Page: http://www.utilmind.com }
{ Created: May, 12, 1999 }
{ Modified: August, 3, 1999 }
{ Legal: Copyright (c) 1999, UtilMind Solutions }
{*************************************************************}
{ TDTCalc (in English) }
{ Component for calculation of amount of years, months, days, }
{ hours, minutes, seconds and miliseconds past between two }
{ time intervals. }
{ PROPERTIES: }
{ StartTime, EndTime: TDateTime; - Range of time interval. }
{ READ ONLY PROPERTIES: }
{ Years: Int64 }
{ Months: Int64 }
{ Days: Int64 }
{ Hours: Int64 }
{ Minutes: Int64 }
{ Seconds: Int64 }
{ MSeconds: Int64 }
{*************************************************************}
{ Please see demo program for more information. }
{*************************************************************}
{ IMPORTANT NOTE: }
{ This software is provided 'as-is', without any express or }
{ implied warranty. In no event will the author be held }
{ liable for any damages arising from the use of this }
{ software. }
{ Permission is granted to anyone to use this software for }
{ any purpose, including commercial applications, and to }
{ alter it and redistribute it freely, subject to the }
{ following restrictions: }
{ 1. The origin of this software must not be misrepresented, }
{ you must not claim that you wrote the original software. }
{ If you use this software in a product, an acknowledgment }
{ in the product documentation would be appreciated but is }
{ not required. }
{ 2. Altered source versions must be plainly marked as such, }
{ and must not be misrepresented as being the original }
{ software. }
{ 3. This notice may not be removed or altered from any }
{ source distribution. }
{*************************************************************}
unit DTCalc;
interface
uses
{$IFDEF Win32} Windows, {$ELSE} WinTypes, WinProcs, {$ENDIF}
SysUtils, Classes;
type
TDTCalc = class(TComponent)
private
FStartTime, FEndTime: TDateTime;
FYears, FMonths, FDays, FHours, FMinutes: Int64;
FSeconds, FMSeconds: Int64;
procedure SetStartTime(Value: TDateTime);
procedure SetEndTime(Value: TDateTime);
procedure Calculate;
procedure SetNone(Value: Int64);
public
property Years: Int64 read FYears;
property Months: Int64 read FMonths;
property Days: Int64 read FDays;
property Hours: Int64 read FHours;
property Minutes: Int64 read FMinutes;
property Seconds: Int64 read FSeconds;
property MSeconds: Int64 read FMSeconds;
published
property StartTime: TDateTime read FStartTime write SetStartTime;
property EndTime: TDateTime read FEndTime write SetEndTime;
end;
procedure Register;
implementation
procedure TDTCalc.SetStartTime(Value: TDateTime);
begin
FStartTime := Value;
Calculate;
end;
procedure TDTCalc.SetEndTime(Value: TDateTime);
begin
FEndTime := Value;
Calculate;
end;
procedure TDTCalc.Calculate;
var
e: Extended;
TempStr: String;
procedure Truncate(var Value: Int64);
begin
try
Value := Trunc(e);
except
Value := -1;
end;
end;
begin
e := MSecsPerDay * (FEndTime - FStartTime);
Truncate(FMSeconds);
TempStr := IntToStr(FMSeconds);
if TempStr[Length(TempStr)] = '9' then inc(FMSeconds);
e := e / 1000;
Truncate(FSeconds);
e := e / 60;
Truncate(FMinutes);
e := e / 60;
Truncate(FHours);
e := e / 24;
Truncate(FDays);
FMonths := Trunc((FEndTime - FStartTime) / 30.4375);
FYears := Trunc((FEndTime - FStartTime) / 365.25);
end;
procedure TDTCalc.SetNone; begin {} end;
procedure Register;
begin
RegisterComponents('UtilMind', [TDTCalc]);
end;
end.