DateTime Round Up and Down

前端 未结 5 1854
面向向阳花
面向向阳花 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条回答
  • 2021-02-07 13:34

    This will let you round according to any interval given.

    public static class DateTimeExtensions
    {
      public static DateTime Floor(this DateTime dateTime, TimeSpan interval)
      {
        return dateTime.AddTicks(-(dateTime.Ticks % interval.Ticks));
      }
    
      public static DateTime Ceiling(this DateTime dateTime, TimeSpan interval)
      {
        var overflow = dateTime.Ticks % interval.Ticks;
    
        return overflow == 0 ? dateTime : dateTime.AddTicks(interval.Ticks - overflow);
      }
    
      public static DateTime Round(this DateTime dateTime, TimeSpan interval)
      {
        var halfIntervalTicks = (interval.Ticks + 1) >> 1;
    
        return dateTime.AddTicks(halfIntervalTicks - ((dateTime.Ticks + halfIntervalTicks) % interval.Ticks));
      }
    }
    

    To take care of truncating the seconds, I would simply subtract the seconds and milliseconds from the date-time before sending them into the rounding functions.

    0 讨论(0)
  • 2021-02-07 13:34

    Here is a fast way to truncate (round down)

    var now = DateTime.Now;
    var nowTicks = now.Ticks;
    
    //removing the nanoseconds, miliseconds, and seconds from the nowTicks
    var lastMinute = new DateTime(nowTicks - (nowTicks % (1000*1000*10*60)));
    
    0 讨论(0)
  • 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);
        }
    
    0 讨论(0)
  • 2021-02-07 13:43

    How about:

    case RoundingDirection.Up:
        t = dt.AddMinutes((60 - dt.Minute) % 10);
    case RoundingDirection.Down:
        t = dt.AddMinutes(-dt.Minute % 10);
    

    Demo: http://ideone.com/AlB7Q

    0 讨论(0)
  • 2021-02-07 13:46

    Another approach avoiding arithmetic using type long.

    Using integer division, where a & b are positive integers:

    a/b             // rounding down 
    (a+b-1)/b       // rounding up 
    ((2*a)+b)/(2*b) // rounding to the nearest (0.5 up)
    

    To round up:

    public static DateTime UpToNearestXmin( DateTime dt, int block )
    {
       int a = dt.Minute;
       int b = block;
    
       int mins = block * (( a + b - 1 ) / b );
    
       return new DateTime( dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0 ).AddMinutes( mins );
    }
    

    To round down or to nearest, change the mins calculation as appropriate.

    The minutes are rounded. The seconds & milliseconds are zeroed which is expected behaviour.

    0 讨论(0)
提交回复
热议问题