16

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 account for the the case when its already rounded.

I need the following rounding to happen

5:00 -> RoundDown() -> 5:00
5:04 -> RoundDown() -> 5:00
5:09 -> RoundDown() -> 5:00
5:10 -> RoundDown() -> 5:10

4:00 -> RoundUp() -> 4:00
4:50 -> RoundUp() -> 4:50
4:51 -> RoundUp() -> 5:00
4:56 -> RoundUp() -> 5:00 

Basically I need it to RoundUp() or RoundDown() to the nearest 10 minutes explicitly but it should also leave time untouched if it already is in a multiple of 10 minutes. Also I'd like to truncate any seconds to that they have no effect on the rounding procedure

4:50:45 -> 4:50:00 -> RoundUp() -> 4:50

Does anyone have any handy code to accomplish this.

I found this code somewhere but it rounds 5:00 -> RoundUp() -> 5:10 rather than leaving it intact because its already a multiple of 10 and needs no rounding. Also Im not sure how seconds would effect it

public static DateTime RoundDateTime(this DateTime dt, int minutes, RoundingDirection direction)
{
    TimeSpan t;
    switch (direction)
    {
        case RoundingDirection.Up:
            t = (dt.Subtract(DateTime.MinValue)).Add(new TimeSpan(0, minutes, 0)); break;
        case RoundingDirection.Down:
            t = (dt.Subtract(DateTime.MinValue)); break;
        default:
            t = (dt.Subtract(DateTime.MinValue)).Add(new TimeSpan(0, minutes / 2, 0)); break;
    }
    return DateTime.MinValue.Add(new TimeSpan(0,
           (((int)t.TotalMinutes) / minutes) * minutes, 0));
}

Hope someone can edit that method to make it work for me. Thanks

parliament
  • 21,544
  • 38
  • 148
  • 238

5 Answers5

44

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.

aj.toulan
  • 1,341
  • 1
  • 16
  • 25
14

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

mellamokb
  • 56,094
  • 12
  • 110
  • 136
2

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)));
Jamie Dunstan
  • 3,725
  • 2
  • 24
  • 38
JasonS
  • 7,443
  • 5
  • 41
  • 61
1

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);
    }
Keith Gresham
  • 153
  • 1
  • 7
1

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.

Kevin Swann
  • 1,018
  • 12
  • 28