In Delphi: How do I round a TDateTime to closest second, minute, five-minute etc?

前端 未结 7 1079
余生分开走
余生分开走 2021-01-02 00:23

Does there exist a routine in Delphi that rounds a TDateTime value to the closest second, closest hour, closest 5-minute, closest half hour etc?

UPDATE:

Gab

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-02 00:56

    If you want to RoundUp or RoundDown ... like Ceil and Floor...

    Here there are (do not forget to add Math unit to your uses clause):

    function RoundUpToNearest(TheDateTime,TheRoundStep:TDateTime):TDateTime;
        begin
             if 0=TheRoundStep
             then begin // If round step is zero there is no round at all
                       RoundUpToNearest:=TheDateTime;
                  end
             else begin // Just round up to nearest bigger or equal multiple of TheRoundStep
                       RoundUpToNearest:=Ceil(TheDateTime/TheRoundStep)*TheRoundStep;
                  end;
        end;
    
    function RoundDownToNearest(TheDateTime,TheRoundStep:TDateTime):TDateTime;
        begin
             if 0=TheRoundStep
             then begin // If round step is zero there is no round at all
                       RoundDownToNearest:=TheDateTime;
                  end
             else begin // Just round down to nearest lower or equal multiple of TheRoundStep
                       RoundDownToNearest:=Floor(TheDateTime/TheRoundStep)*TheRoundStep;
                  end;
        end;
    

    And of course with a minor change (use Float type instead of TDateTime type) if can also be used to Round, RoundUp and RoundDown decimal/float values to a decimal/float step.

    Here they are:

    function RoundUpToNearest(TheValue,TheRoundStep:Float):Float;
        begin
             if 0=TheRoundStep
             then begin // If round step is zero there is no round at all
                       RoundUpToNearest:=TheValue;
                  end
             else begin // Just round up to nearest bigger or equal multiple of TheRoundStep
                       RoundUpToNearest:=Ceil(TheValue/TheRoundStep)*TheRoundStep;
                  end;
        end;
    
    function RoundToNearest(TheValue,TheRoundStep:Float):Float;
        begin
             if 0=TheRoundStep
             then begin // If round step is zero there is no round at all
                       RoundToNearest:=TheValue;
                  end
             else begin // Just round to nearest multiple of TheRoundStep
                       RoundToNearest:=Floor(TheValue/TheRoundStep)*TheRoundStep;
                  end;
        end;
    
    function RoundDownToNearest(TheValue,TheRoundStep:Float):Float;
        begin
             if 0=TheRoundStep
             then begin // If round step is zero there is no round at all
                       RoundDownToNearest:=TheDateTime;
                  end
             else begin // Just round down to nearest lower or equal multiple of TheRoundStep
                       RoundDownToNearest:=Floor(TheValue/TheRoundStep)*TheRoundStep;
                  end;
        end;
    

    If you want to use both types (TDateTime and Float) on same unit... add overload directive on interface section, example:

    function RoundUpToNearest(TheDateTime,TheRoundStep:TDateTime):TDateTime;overload;
    function RoundToNearest(TheDateTime,TheRoundStep:TDateTime):TDateTime;overload;
    function RoundDownToNearest(TheDateTime,TheRoundStep:TDateTime):TDateTime;overload;
    
    function RoundUpToNearest(TheValue,TheRoundStep:Float):Float;overload;
    function RoundToNearest(TheValue,TheRoundStep:Float):Float;overload;
    function RoundDownToNearest(TheValue,TheRoundStep:Float):Float;overload;
    

提交回复
热议问题