DateTime Round Up and Down

前端 未结 5 1855
面向向阳花
面向向阳花 2021-02-07 12:58

Ive been looking for a proper rounding mechanism but nothing I find seems to be exactly what I need.

I need to round up and round down seperately and I also need to acco

5条回答
  •  -上瘾入骨i
    2021-02-07 13:40

    This function will round up or down to the nearest interval (minutes).

        private static DateTime NormalizeReadingInterval(DateTime originalTime, int interval)
        {
            if (originalTime.Minute % interval == 0) return originalTime;
            var epochTime = new DateTime(1900, 1, 1);
            var minutes = (originalTime - epochTime).TotalMinutes;
            var numIntervals = minutes / interval;
            var roundedNumIntervals = Math.Round(numIntervals, 0);
            return epochTime.AddMinutes(roundedNumIntervals * interval);
        }
    

提交回复
热议问题