How to deal with Rounding-off TimeSpan?

前端 未结 3 1342
刺人心
刺人心 2021-01-02 15:23

I take the difference between two DateTime fields, and store it in a TimeSpan variable, Now I have to round-off the TimeSpan by the following rules:

if the minutes i

3条回答
  •  臣服心动
    2021-01-02 15:50

    You can use

    double v = span.TotalHours;     
    v = Math.Round(v, MidpointRounding.AwayFromZero);
    span = TimeSpan.FromHours(v);
    

    It depends on whether I understood your rules for negative values correctly.

提交回复
热议问题