Rundet eine Datum-Zeit angaben auf ein angegebenes Intervall (15 Minuten,Halbe Stunde, usw...)
/// <summary>
/// The Rounding types
/// </summary>
public enum RoundType
{
/// <summary>
/// Complete Hour
/// </summary>
Hour = 60,
/// <summary>
/// Half an Hour
/// </summary>
HalfHour = 30,
/// <summary>
/// 15 Miniutes
/// </summary>
QuaterHour = 15,
}
/// <summary>
/// Roundes the given DateTime to the given Minutes Pattern (15,30,60...)
/// </summary>
/// <param name="source">DateTime which should be rounded</param>
/// <param name="type">The Roundtype</param>
public static System.DateTime Round(this System.DateTime source, RoundType type)
{
System.DateTime result = source;
int minutes = Convert.ToInt32(type);
int modulo;
Math.DivRem(source.Minute, minutes, out modulo);
if (modulo > 0)
{
if (modulo >= (minutes / 2))
{
//Näher am oberen ende => nach utnen laufen
result = source.AddMinutes(minutes - modulo);
}
else
{
//Wir sind näher am unteren ende => bis zur unteren basis laufen
result = source.AddMinutes(modulo * -1);
}
result = result.AddSeconds(source.Second * -1);
}
return result;
}
1 Kommentare zum Snippet