If you only need to go to a specific device, I probably would not even bother to use Math.Round or Math.Floor - I would 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(); }
This does not work if you need "the next 5 minutes", etc., but for one block of time it is easier to understand and debug than to try to make arithmetic work.
Alternatively, as another answer to the accepted answer to the question you are referring to, you can simply do:
// tickCount is the rounding interval, eg TimeSpan.FromMinutes(5).Ticks DateTime rounded = new DateTime((old.Ticks / tickCount) * tickCount);
Please note that this will not help round up to the start of the month or year.