In this answer of a similar question the DateTime is rounded to the closes (time) boundary,
the Math.Round
method does not allow round to the lower boundary by choice.
Is there a way to calculate the same way the lower boundary of some time ?
Meaning if the time is 10/2/2012 10:52:30 and the choice is an hour than the time is : 10/2/2012 10:00:00, if the choice is day than 10/2/2012 00:00:00 and so on.
Asked
Active
Viewed 3,411 times
4
-
Are you planning to use your `DateTime` value in arithmetic later on or just display it rounded? – M.Babcock Jan 08 '12 at 08:36
-
Yes, i would use it in an sql query – guyl Jan 08 '12 at 08:40
-
Then you likely need a string anyway. Why not just use a variation of @JonSkeet's answer or just `DateTime.ToString()`? – M.Babcock Jan 08 '12 at 08:41
2 Answers
6
If you only need to go to a particular unit, I probably wouldn't even bother using Math.Round
or Math.Floor
- I'd just go with something like:
switch (unitToRoundDownTo)
{
case Unit.Second:
return new DateTime(old.Year, old.Month, old.Day,
old.Hour, old.Minute, old.Second, old.Kind);
case Unit.Minute:
return new DateTime(old.Year, old.Month, old.Day,
old.Hour, old.Minute, 0, old.Kind);
case Unit.Hour:
return new DateTime(old.Year, old.Month, old.Day, old.Hour, 0, 0, old.Kind);
case Unit.Day:
return new DateTime(old.Year, old.Month, old.Day, 0, 0, 0, old.Kind);
case Unit.Month:
return new DateTime(old.Year, old.Month, 1, 0, 0, 0, old.Kind);
case Unit.Year:
return new DateTime(old.Year, 1, 1, 0, 0, 0, old.Kind);
default:
throw new ArgumentOutOfRangeException();
}
That doesn't work if you need "the nearest 5 minutes" etc, but for a single time unit it's simpler to understand and debug than trying to get arithmetic to work.
Alternatively, as a different spin on the accepted answer to the question you link to, you can just do:
// tickCount is the rounding interval, e.g. TimeSpan.FromMinutes(5).Ticks
DateTime rounded = new DateTime((old.Ticks / tickCount) * tickCount);
Note that this won't help for rounding to the start of the month or year.

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194
-
It's probably a good idea to maintain the kind as well, which you can accomplish by adding `, old.Kind` as the final parameter to the DateTime constructor. – Brad Jun 24 '15 at 22:17
-
@Brad: Yes, that's true. Will edit. (Not that I'm keen on the kind part, but...) – Jon Skeet Jun 25 '15 at 05:25
3
Try Math.Floor
instead of Math.Round
(in a similar fashion to the post you linked).

Eve Freeman
- 32,467
- 4
- 86
- 101