Mega Code Archive

 
Categories / Delphi / Examples
 

Rounding Time To A Quarter

Title: Rounding Time To A Quarter Question: I need a function like RoundTime or so, that rounds a given timestamp to a quarter.. for example if i have 12:08 i need 12:15:00 Answer: To round off a time to the nearest increment, use the following function: function RoundTime(T : TDateTime; Increment : Integer) : TDateTime; var IncrementFraction : Double; begin IncrementFraction := Increment / 86400; Result := round(T / IncrementFraction) * IncrementFraction; end; Increment is the number of seconds that you want the resulting DateTime to be a multiple of. For instance: RoundTime(StrToDateTime('10/09/99 05:15:05'), 1800) will return '10/09/78 05:30:00', because the time is rounded off to the nearest 1,800 seconds. RoundTime(StrToDateTime('01/05/89 03:15:05'), 3600) will return '01/05/89 03:00:00'.