Mega Code Archive

 
Categories / Delphi / Functions
 

Rounding Time, RoundToQuarter function

Title: Rounding Time, RoundToQuarter function Question: Response to article request: I need a fuction like RoundTime or so, that rounds a given timestamp to a quarter. Answer: function RoundToQuarter(dTime: TDateTime): TDateTime; var Hour, Min, Sec, MSec: Word; begin DecodeTime(dTime, Hour, Min, Sec, MSec); if Sec 30 then Min := Min + 1; case Min of 0..7 : Min := 0; 8..22: Min := 15; 23..37: Min := 30; 38..52: Min := 45; 53..59: begin Min := 0; Hour := Hour + 1; end; end; if Hour = 24 then Hour := 0; Sec := 0; MSec := 0; Result := EncodeTime(Hour, Min, Sec, MSec); end; // example of using the function procedure TForm1.Button3Click(Sender: TObject); begin DateTimePicker1.Time := RoundToQuarter(DateTimePicker1.Time); end;