Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subtracting Time

Status
Not open for further replies.

EricDraven

Programmer
Jan 17, 2002
2,999
GB
Does anybody know the easiest way to subtract a length of time from a give time to leave the new time?

For example :-

17:56 - 2.5 hours = 15:26

It sounds simple enought to do but I cant find anybody who can tell me a quick and simple way to accomplish this. Also when the time entered is 00:30 (for example), the system needs to know that the returned time is for the previous day. Any ideas? Anybody?
 
It would be easy to convert the hour to minutes (from midnight) and do a simple substraction:

17:56 = 17*60+56 = 1076 minutes
2.5 hours = 2.5*60 = 150 minutes
17:56 - 2.5 = 1076 - 150 = 926 minutes

Now convert 926 minutes to hh:mm :
hh:=926 div 60; //=15
mm:=926 mod 60; //=26

If the difference is negative, then the new time is one day earlier (actually is (difference div 1440) + 1 days earlier).

Hope it's what you're looking for...
 
Check out the TDate and TDateTime types in the help file S. van Els
SAvanEls@cq-link.sr
 
Have checked out the TDate and TDateTime routines in the help files. They offer absolutley no help whatsoever this time but thanks anyway.
 
It depends how you want to define hours and minutes.
17:56 has a base of 60 minutes,
but the second time definition (2.5) has a base of 100.


This tiny example works if you are using correct time definitions:

procedure TForm1.Button1Click(Sender: TObject);
var
t1, t2: TTime;
begin
t1 := StrToTime('17:56');
t2 := StrToTime('2:30');
showmessage(TimeToStr(t2-t1));
end;

Andreas Nordlund
Software developer
 
My suggestion would be to convert the minutes into a standard Delphi Date/Time and then just subtract.

To convert the minutes to a DateTime just divide the number of minutes by (60*24).

i.e.
function subtractMinutesFromDateTime(Time1 : TDateTime;Minutes : extended) : TDateTime;
var
TimeMinutes : TDateTime;
const
MinutesPerDay = 60 * 24;
begin
TimeMinutes := Minutes / MinutesPerDay;
result := time1 - TimeMinutes;
end;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top