有舍入一个简单的函数 UP 一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}