c# - Roundoff Timespan to 15 min interval -


i have property in code users can enter timespan in hh:mm

10:32 10:44 15:45 

i want round off in property nearest 15mins dont have datetime here. need timespan

10:32 10:30 10:44 10:45 15:45 15:45 01:02 01:00 02:11 02:15 03:22 03:15 23:52 00:00 

tried these solutions involve datetime in them

how can round time nearest x minutes?
is there simple function rounding datetime down nearest 30 minutes, in c#?
dotnet roundoff datetime last 15 minutes

i think want this:

public static class timespanextensions {     public static timespan roundtonearestminutes(this timespan input, int minutes)     {         var totalminutes = (int)(input + new timespan(0, minutes/2, 0)).totalminutes;          return new timespan(0, totalminutes - totalminutes % minutes, 0);     } } 

if pass in 15 chosen interval rounding, function first add 7 mins, round down nearest 15 mins. should give want.

because above written extension method, can use this:

var span1 = new timespan(0, 10, 37, 00).roundtonearestminutes(15); var span2 = new timespan(0, 10, 38, 00).roundtonearestminutes(15); 

the first 1 becomes 10:30, , second 1 becomes 10:45 (as desired).


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -