是否有一个简单的函数可以将a 向上DateTime舍入到最接近的 15 分钟?
DateTime
例如
2011-08-11 16:59变成2011-08-11 17:00
2011-08-11 16:59
2011-08-11 17:00
2011-08-11 17:00保持为2011-08-11 17:00
2011-08-11 17:01变成2011-08-11 17:15
2011-08-11 17:01
2011-08-11 17:15
DateTime RoundUp(DateTime dt, TimeSpan d) { return new DateTime((dt.Ticks + d.Ticks - 1) / d.Ticks * d.Ticks, dt.Kind); }
例子:
var dt1 = RoundUp(DateTime.Parse("2011-08-11 16:59"), TimeSpan.FromMinutes(15)); // dt1 == {11/08/2011 17:00:00} var dt2 = RoundUp(DateTime.Parse("2011-08-11 17:00"), TimeSpan.FromMinutes(15)); // dt2 == {11/08/2011 17:00:00} var dt3 = RoundUp(DateTime.Parse("2011-08-11 17:01"), TimeSpan.FromMinutes(15)); // dt3 == {11/08/2011 17:15:00}