Round to Nearest TimeSpan

public static TimeSpan RoundToNearest(this TimeSpan a, TimeSpan roundTo){
		long ticks = (long)(Math.Round(a.Ticks / (double)roundTo.Ticks) * roundTo.Ticks);
		return new TimeSpan(ticks);
	}
Example:
TimeSpan example = DateTime.Now.TimeOfDay;
Console.WriteLine(example.RoundToNearest(TimeSpan.FromMinutes(15)));

Description

Rounds a TimeSpan value to the nearest timespan given

Details

Double click on the code to select all.

 

;