Solved

Add 5 minutes

Posted on 2001-09-13
6
370 Views
Last Modified: 2010-04-06
Simple question:

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

Thanks
0
Comment
Question by:olisaac
  • 4
6 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6480523
datetime := datetime + 1/(24*20);
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6480528
sorry

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

Expert Comment

by:kretzschmar
ID: 6480586
another method, better readable

const
  MinutesPerDay = 1440;

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

meikl ;-)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Accepted Solution

by:
Jacco earned 30 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);

function DateTimeAdd(aDateTime: TDateTime; aTimePart: TTimePart; aQuantity: Integer): TDateTime;
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);
  aTime := TimeStampAdd(aTime, tpMinute, 5);
  Memo1.Lines.Add(DateTimeToStr(TimeStampToDateTime(aTime)));
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  aTime: TDateTime;
begin
  aTime := Now;
  aTime := DateTimeAdd(aTime, tpMinute, 5);
  Memo1.Lines.Add(DateTimeToStr(aTime));
end;
0
 
LVL 1

Author Comment

by:olisaac
ID: 6481153
kretzschmar, your answers don't give me the right value

It's ok Jacco

Thank you Jacco and kretzschmar for your quick answers
0
 
LVL 27

Expert Comment

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

meikl ;-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question