Comment puis-je arrondir le temps aux X minutes les plus proches?

Existe-t-il une fonction simple pour arrondir un DateTime à 15 minutes près?

Par exemple

2011-08-11 16:59 devient 2011-08-11 17:00

2011-08-11 17:00 rest en tant que 2011-08-11 17:00

2011-08-11 17:01 devient 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); } 

Exemple:

 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} 

Entré avec une solution qui n’implique pas la multiplication et la division long nombres long .

 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); } 

Usage:

 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 

si vous avez besoin d’arrondir à un intervalle de temps le plus proche (pas en haut) alors je suggère d’utiliser les éléments suivants

  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); } } 

Résultats:

 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 

Comme je déteste réinventer la roue, je suivrais probablement cet algorithme pour arrondir une valeur DateTime à un incrément de temps spécifié (Timespan):

  • Convertissez la valeur DateTime pour qu’elle soit arrondie à une valeur décimale représentant un nombre entier et fractionnaire d’unités TimeSpan .
  • Math.Round() cela à un entier en utilisant Math.Round() .
  • Redimensionnez les ticks en multipliant le nombre entier arrondi par le nombre de ticks dans l’unité TimeSpan .
  • Instanciez une nouvelle valeur DateTime partir du nombre arrondi de graduations et renvoyez-la à l’appelant.

Voici le code:

 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 ; } } 

Élégant?

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

Attention: la formule ci-dessus est incorrecte, à savoir:

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

devrait être réécrit comme:

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

Une solution plus verbeuse, qui utilise modulo et évite les calculs inutiles.

 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); } } 

Ceci est une solution simple pour arrondir à la minute près. Il conserve les informations TimeZone et Kind de DateTime. Il peut être modifié pour répondre à vos propres besoins (si vous devez arrondir à 5 minutes près, etc.).

 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; 

Vous pouvez utiliser cette méthode, elle utilise la date spécifiée pour garantir qu’elle conserve les types de globalisation et de date / heure précédemment spécifiés dans l’object datetime.

 const long LNG_OneMinuteInTicks = 600000000; ///  /// Round the datetime to the nearest minute ///  ///  /// The number minute use to round the time to ///  public static DateTime Round(DateTime dateTime, int numberMinutes = 1) { long roundedMinutesInTicks = LNG_OneMinuteInTicks * numberMinutes; long remainderTicks = dateTime.Ticks % roundedMinutesInTicks; if (remainderTicks < roundedMinutesInTicks / 2) { // round down return dateTime.AddTicks(-remainderTicks); } // round up return dateTime.AddTicks(roundedMinutesInTicks - remainderTicks); } 

Test de violon .Net

Si vous souhaitez utiliser le TimeSpan pour arrondir, vous pouvez l'utiliser.

 ///  /// Round the datetime ///  /// Round(dt, TimeSpan.FromMinutes(5)); => round the time to the nearest 5 minutes. ///  /// The time use to round the time to ///  public static DateTime Round(DateTime dateTime, TimeSpan roundBy) { long remainderTicks = dateTime.Ticks % roundBy.Ticks; if (remainderTicks < roundBy.Ticks / 2) { // round down return dateTime.AddTicks(-remainderTicks); } // round up return dateTime.AddTicks(roundBy.Ticks - remainderTicks); } 

Violon TimeSpan