我怎样才能把时间整理到最近的X分钟?

是否有一个简单的函数来将DateTime时间舍入到最近的15分钟?

例如

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

 DateTime RoundUp(DateTime dt, TimeSpan d) { return new DateTime(((dt.Ticks + d.Ticks - 1) / d.Ticks) * d.Ticks); } 

例:

 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} 

提出了一个不涉及乘数long整数分解的解决scheme。

 public static DateTime RoundUp(this DateTime dt, TimeSpan d) { var modTicks = dt.Ticks % d.Ticks; var delta = modTicks != 0 ? d.Ticks - modTicks : 0; return new DateTime(dt.Ticks + delta, dt.Kind); } public static DateTime RoundDown(this DateTime dt, TimeSpan d) { var delta = dt.Ticks % d.Ticks; return new DateTime(dt.Ticks - delta, dt.Kind); } public static DateTime RoundToNearest(this DateTime dt, TimeSpan d) { var delta = dt.Ticks % d.Ticks; bool roundUp = delta > d.Ticks / 2; var offset = roundUp ? d.Ticks : 0; return new DateTime(dt.Ticks + offset - delta, dt.Kind); } 

用法:

 var date = new DateTime(2010, 02, 05, 10, 35, 25, 450); // 2010/02/05 10:35:25 var roundedUp = date.RoundUp(TimeSpan.FromMinutes(15)); // 2010/02/05 10:45:00 var roundedDown = date.RoundDown(TimeSpan.FromMinutes(15)); // 2010/02/05 10:30:00 var roundedToNearest = date.RoundToNearest(TimeSpan.FromMinutes(15)); // 2010/02/05 10:30:00 

如果你需要四舍五入到最近的时间间隔(不起来),那么我build议使用以下内容

  static DateTime RoundToNearestInterval(DateTime dt, TimeSpan d) { int f=0; double m = (double)(dt.Ticks % d.Ticks) / d.Ticks; if (m >= 0.5) f=1; return new DateTime(((dt.Ticks/ d.Ticks)+f) * d.Ticks); } 
 void Main() { var date1 = new DateTime(2011, 8, 11, 16, 59, 00); date1.Round15().Dump(); var date2 = new DateTime(2011, 8, 11, 17, 00, 02); date2.Round15().Dump(); var date3 = new DateTime(2011, 8, 11, 17, 01, 23); date3.Round15().Dump(); var date4 = new DateTime(2011, 8, 11, 17, 00, 00); date4.Round15().Dump(); } public static class Extentions { public static DateTime Round15(this DateTime value) { var ticksIn15Mins = TimeSpan.FromMinutes(15).Ticks; return (value.Ticks % ticksIn15Mins == 0) ? value : new DateTime((value.Ticks / ticksIn15Mins + 1) * ticksIn15Mins); } } 

结果:

 8/11/2011 5:00:00 PM 8/11/2011 5:15:00 PM 8/11/2011 5:15:00 PM 8/11/2011 5:00:00 PM 

由于我讨厌重新发明轮子,所以我可能会遵循这个algorithm将DateTime值舍入为指定的时间增量(Timespan):

  • DateTime值转换为十进制浮点值,表示TimeSpan单位的整数和小数。
  • 使用Math.Round()将其Math.Round()入为整数。
  • 通过将四舍五入的整数乘以TimeSpan单位中的节拍数量,缩小回到滴答声。
  • 从四舍五入的数字中实例化新的DateTime值并将其返回给调用者。

代码如下:

 public static class DateTimeExtensions { public static DateTime Round( this DateTime value , TimeSpan unit ) { return Round( value , unit , default(MidpointRounding) ) ; } public static DateTime Round( this DateTime value , TimeSpan unit , MidpointRounding style ) { if ( unit <= TimeSpan.Zero ) throw new ArgumentOutOfRangeException("unit" , "value must be positive") ; Decimal units = (decimal) value.Ticks / (decimal) unit.Ticks ; Decimal roundedUnits = Math.Round( units , style ) ; long roundedTicks = (long) roundedUnits * unit.Ticks ; DateTime instance = new DateTime( roundedTicks ) ; return instance ; } } 

优雅?

 dt.AddSeconds(900 - (x.Minute * 60 + x.Second) % 900) 

注意:上面的公式不正确,即:

 DateTime RoundUp(DateTime dt, TimeSpan d) { return new DateTime(((dt.Ticks + d.Ticks - 1) / d.Ticks) * d.Ticks); } 

应改写为:

 DateTime RoundUp(DateTime dt, TimeSpan d) { return new DateTime(((dt.Ticks + d.Ticks/2) / d.Ticks) * d.Ticks); } 

一个更详细的解决scheme,使用模数,并避免不必要的计算。

 public static class DateTimeExtensions { public static DateTime RoundUp(this DateTime dt, TimeSpan ts) { return Round(dt, ts, true); } public static DateTime RoundDown(this DateTime dt, TimeSpan ts) { return Round(dt, ts, false); } private static DateTime Round(DateTime dt, TimeSpan ts, bool up) { var remainder = dt.Ticks % ts.Ticks; if (remainder == 0) { return dt; } long delta; if (up) { delta = ts.Ticks - remainder; } else { delta = -remainder; } return dt.AddTicks(delta); } } 

这是一个简单的解决scheme,可以精确到1分钟。 它保留了DateTime的TimeZone和Kind信息。 它可以进行修改,以进一步满足自己的需要(如果你需要四舍五入到最近的5分钟等)。

 DateTime dbNowExact = DateTime.Now; DateTime dbNowRound1 = (dbNowExact.Millisecond == 0 ? dbNowExact : dbNowExact.AddMilliseconds(1000 - dbNowExact.Millisecond)); DateTime dbNowRound2 = (dbNowRound1.Second == 0 ? dbNowRound1 : dbNowRound1.AddSeconds(60 - dbNowRound1.Second)); DateTime dbNow = dbNowRound2;