Solved

Add 5 minutes

Posted on 2001-09-13
6
367 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE2 application frozen on Windows 10 10 270
Run video youtube webbrowse 10 57
oracle global variables 4 63
Graphics32 under Delphi 10.1 Berlin 2 34
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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

919 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now