DateTime Round Up and Down
Solution 1
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
Solution 2
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.
Solution 3
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)));
Solution 4
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);
}
Solution 5
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.
parliament
Serial Entrepreneur Full Stack Developer (C#/AngularJS/D3.js) Contact me if you want to work on cool shit with me. Currently in the bitcoin space, starting a cryptocurrency exchange. Also have a body of work on automated trading systems. [email protected]
Updated on November 06, 2020Comments
-
parliament over 3 years
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
-
comecme almost 12 yearsDoes that take care of the seconds?