Is there a better way in C# to round a DateTime to the nearest 5 seconds?

后端 未结 7 1017
半阙折子戏
半阙折子戏 2020-12-09 04:33

I want to round a DateTime to the nearest 5 seconds. This is the way I\'m currently doing it but I was wondering if there was a better or more concise way?

         


        
7条回答
  •  醉梦人生
    2020-12-09 05:10

    Technically, you can never correctly round to an odd interval given only seconds.

    2, 4, 6, 8, 10 <-- are no problem

    If you are 'distributing' times in intervals and if the jitter is low, truncation is a lot more tractable.

    If you can pass milliseconds and round at a 500mS mark, you will be able to to odd seconds and also slash the effect of jitter way down or eliminate it entirely.

提交回复
热议问题