Round Numbers. Make the Time numbers nice

Hello Guys
I hope you are well

I have to round these times to nice numbers:

If its 13 becomes 15, to nearest quarter.
I can read the text and convert it to integer and split it to minutes, and hour.
How to round please help?

Do i have to use 100 if statements?

Thank You

one of many approaches

one question left: do you also looking for down rounding, like 15:17 → 15:15?

1 Like

Thank You Peter.
I am looking for:

08:13 → 08.15
14:17 → 14:15
19:29 → 19:30
13:24 → 13:25

Hi Hurmet,

how about this:

MinutesRoundedToQuarter (Type Double) = Math.Ceiling(SampleMinutes / 15) * 15

OH! You want to round down too…

MinutesRoundedToQuarter (Type Double) = Math.Round(SampleMinutes / 15) * 15


Now I have to check if its 60 then add one more to the time.

if i have 19:34

How can i substring “9”

Thank You Lukaszie

have a look on my samples as this was exactly handling tem 60 min case und jumps up to next hour

1 Like

Oh what, wait. That’s not good.

You should use DateTime.AddMinutes instead then! No more time right now, I’ll try later

1 Like

I am looking also for down …
When its 18:11 should be 18:10

Thank You

combine the approach from Lukasz and mine. Just give me a little time

1 Like

find starter help:
RoundTimesToClosest15MinSegment.xaml (7.7 KB)


Thank You Peter
I will test it now


I am looking for 5 minutes segment.

11 becomes 10
14 becomes 15
27 becomes 25
32 becomes 30

Thank You

so you are looking for a round to the closest 5 segement and not 15?

tsInit.AddMinutes(- tsInit.Minute).AddMinutes(Math.Round(tsInit.Minute / 5) * 5).toString(“HH:mm”)


Yes Sir

This works. I changed 15 to 5 and it works.

Thank You MAN!

Only thing is how to convert “08:30” string to date?

I had to finish this :smiley:

RoundedTime = SampleTime.AddMinutes((Math.Round(SampleTime.Minute / MinuteInterval ) * MinuteInterval) - SampleTime.Minute)

You can convert strings to dates a number of ways. Lots of examples in the forum. Here is one:

SampleDateTime = DateTime.ParseExact(SampleString,"HH:mm",CultureInfo.InvariantCulture)



You are correct, this Works


All the best

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.