Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Time to a TDateTime

Tags:

delphi

I would like to add seconds to a TDateTime variable, so that the result is the top of the minute. For example, if it's 08:30:25, I want change the TDateTime variable to store 08:31:00.

I see that TDateTime has a Decode function, which I could use. There isn't, however, an encode function to put the altered time back into a TDateTime variable.

like image 265
Mike Jablonski Avatar asked May 20 '13 22:05

Mike Jablonski


2 Answers

Using DateUtils it's possible to do it like this:

Uses
  DateUtils;

var
  Seconds : Word;    

Seconds := SecondOfTheMinute(MyTime);  // Seconds from last whole minute
// Seconds := SecondOf(MyTime); is equivalent to SecondOfTheMinute()
if (Seconds > 0) then
  MyTime := IncSecond(MyTime,60 - Seconds);
like image 157
LU RD Avatar answered Oct 10 '22 23:10

LU RD


There sure is, at least in the recent versions - see the DateUtils unit, especially all the Recode* routines and EncodeDateTime. The DateUtils unit is already available in Delphi 2010, perhaps even in earlier version.

like image 44
ain Avatar answered Oct 10 '22 21:10

ain