Go Premium for a chance to win a PS4. Enter to Win

x
Solved

Posted on 2001-09-13
Medium Priority
375 Views
Simple question:

how can I add 5 minutes to a TdateTime variable
and same question for a timestamp variable

Thanks
0
Question by:olisaac
• 4

LVL 27

Expert Comment

ID: 6480523
datetime := datetime + 1/(24*20);
0

LVL 27

Expert Comment

ID: 6480528
sorry

datetime := datetime + 1/(24*12);
0

LVL 27

Expert Comment

ID: 6480586

const
MinutesPerDay = 1440;

DateTime := DateTime + 1/(MinutesPerDay*5);

meikl ;-)
0

LVL 10

Accepted Solution

Jacco earned 120 total points
ID: 6480698
Hi there,

The TTimeStamp question is a little different.

Here is some code I cooked up:

Regards Jacco

const
HoursPerDay           =   24;
MinutesPerHour        =   60;
SecondsPerMinute      =   60;
MilliSecondsPerSecond = 1000;

MilliSecondsPerDay    = HoursPerDay * MinutesPerHour * SecondsPerMinute * MilliSecondsPerSecond; // also defined in SysUtils
MilliSecondsPerHour   = MinutesPerHour * SecondsPerMinute * MilliSecondsPerSecond;
MilliSecondsPerMinute = SecondsPerMinute * MilliSecondsPerSecond;

DayValue          = 1;
HourValue         = DayValue    / HoursPerDay;
MinuteValue       = HourValue   / MinutesPerHour;
SecondValue       = MinuteValue / SecondsPerMinute;
MilliSecondValue  = SecondValue / MilliSecondsPerSecond;

type
TTimePart = (tpHour, tpMinute, tpSecond, tpMilliSecond);

begin
case aTimePart of
tpHour       : Result := aDateTime + aQuantity * HourValue;
tpMinute     : Result := aDateTime + aQuantity * MinuteValue;
tpSecond     : Result := aDateTime + aQuantity * SecondValue;
tpMilliSecond: Result := aDateTime + aQuantity * MilliSecondValue;
end;
end;

function TimeStampAdd(aTimeStamp: TTimeStamp; aTimePart: TTimePart; aQuantity: Integer): TTimeStamp;
begin
Result.Date := aTimeStamp.Date;
case aTimePart of
tpHour       : Result.Time := aTimeStamp.Time + aQuantity * MilliSecondsPerHour;
tpMinute     : Result.Time := aTimeStamp.Time + aQuantity * MilliSecondsPerMinute;
tpSecond     : Result.Time := aTimeStamp.Time + aQuantity * MilliSecondsPerSecond;
tpMilliSecond: Result.Time := aTimeStamp.Time + aQuantity;
end;
Result.Date := Result.Date + aTimeStamp.Time div MilliSecondsPerDay;
Result.Time := Result.Time mod MilliSecondsPerDay;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
aTime: TTimeStamp;
begin
aTime := DateTimeToTimeStamp(Now);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
aTime: TDateTime;
begin
aTime := Now;
end;
0

LVL 1

Author Comment

ID: 6481153

It's ok Jacco

0

LVL 27

Expert Comment

ID: 6481572
as you mean,
(of course my first comment was wrong (typo))

meikl ;-)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…