Q: Rounding time

  • Thread starter Thread starter Geoff Jones
  • Start date Start date
G

Geoff Jones

Hi

Is there a way to round up or down time intervals. For example, suppose we
have a variable storing the time 14:06, what is the easiest way to round it
to the nearest minute?

Thanks in advance

Geoff
 
Apologies, the 14 was minutes and 6 seconds i.e. this would be rounded to 15
minutes.

Geoff
 
I should apologize. It's Monday. :^)

Your variable, is it just a string or a timespan?

Greg
 
Geoff Jones said:
Apologies, the 14 was minutes and 6 seconds i.e. this would be rounded to 15
minutes.

Most rounding routines work on .5 being the next "rounded up" meaning 14.45
would round to 14 but 14.55 would round to 15. If you want everything above
14 (including 14.01) rounded up then just do something:

if int(min) <> min then result = int(min)+1
 
It could be a date object, string or timespan. Could it be something to do
with a cast?

Geoff
 
You have to parse your string to get it into a timespan variable first

(untested)

Function RoundTimeUp(ByVal t As TimeSpan) As TimeSpan
If t.Seconds Mod 60 = 0 Then
Return t
Else
Return New TimeSpan(t.Hours, t.Minutes + 1, 0)
End If
End Function

HTH,
Greg
 
Actually, you don't even need the Mod 60 now that I think about it.
(TimeSpan.Seconds property is always 0-59)

Function RoundTimeUp(ByVal t As TimeSpan) As TimeSpan
If t.Seconds = 0 Then
Return t
Else
Return New TimeSpan(t.Hours, t.Minutes + 1, 0)
End If
End Function
 
Many thanks guys for all your suggestions

Geoff

Ricky W. Hunt said:
to

Most rounding routines work on .5 being the next "rounded up" meaning 14.45
would round to 14 but 14.55 would round to 15. If you want everything above
14 (including 14.01) rounded up then just do something:

if int(min) <> min then result = int(min)+1
 
Back
Top