我想将秒添加到TDateTime变量中,这样结果就是分钟的最高值。例如,如果是08:30:25,我希望将TDateTime变量更改为store 08:31:00。
我看到TDateTime有一个解码函数,我可以使用它。但是,没有encode函数可以将更改后的时间放回TDateTime变量中。
发布于 2013-05-21 06:12:52
肯定有,至少在最近的版本中-请参阅DateUtils单元,特别是所有的Recode*例程和EncodeDateTime。DateUtils单元已经在Delphi2010中可用,甚至可能在更早的版本中。
发布于 2013-05-21 06:29:08
使用DateUtils可以这样做:
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);发布于 2013-05-21 22:06:34
理论
TDateTime数据类型将自1899年12月30日以来的天数表示为实数。也就是说,TDateTime的整数部分是一整天的量,小数部分表示一天中的某个时间。
实用
因此,您的问题可以使用简单的算术来解决:
var
Days: TDateTime;
Mins: Extended; { widen TDateTime's mantissa by 11 bits to accommodate division error }
begin
Days := Date + StrToTime('08:30:25');
Writeln(DateTimeToStr(Days));
Mins := Days * 24 * 60 ; // compute minutes
Mins := Math.Ceil(Mins); // round them up
Days := Mins / (24 * 60); // and back to days
{ or as simple and concise expression as: }
// Days := Ceil(Days * MinsPerDay) / MinsPerDay;
Writeln(DateTimeToStr(Days));https://stackoverflow.com/questions/16659169
复制相似问题