Round to nearest quarter of hour
-
Hi, I need to round the minutes to the nearest queater of hour. For example if it is 00:07 (hour:minutes) then it would be 00:15, if 00:05 then it would be 00:00. Any ideas how to go about doing this? Many thanks for your time.
you can use an if elseif then statement to accomplish this task or even a select case. Are you pulling the time from the Now function and if so then you will need another var to hold the rounded time value.
Recreating the wheel is the best way to appreciate what the previous coders have gone through to get you where you are now.
-
you can use an if elseif then statement to accomplish this task or even a select case. Are you pulling the time from the Now function and if so then you will need another var to hold the rounded time value.
Recreating the wheel is the best way to appreciate what the previous coders have gone through to get you where you are now.
Here is an example of what I was talking about using the if elseif then statement. This assumes you have two labels on a form named Label2 and Label3.
Private Sub Load_Time()
'calculates the rounded time
Dim newtime As DateTime
Dim min As Integer = Now.Minute
Label2.Text = Now.ToShortTimeString
If min >= 0 And min <= 15 Then 'First Quarter
If min > 7 Then
'move up
newtime = New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, 15, Now.Second)
Label3.Text = newtime.ToShortTimeString
Else
'move down
newtime = New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, 0, Now.Second)
Label3.Text = newtime.ToShortTimeString
End If
ElseIf min > 15 And min <= 30 Then 'Second Quarter
If min > 22 Then
'move up
newtime = New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, 30, Now.Second)
Label3.Text = newtime.ToShortTimeString
Else
'move down
newtime = New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, 15, Now.Second)
Label3.Text = newtime.ToShortTimeString
End If
ElseIf min > 30 And min <= 45 Then 'Third Quarter
If min > 37 Then
'move up
newtime = New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, 45, Now.Second)
Label3.Text = newtime.ToShortTimeString
Else
'move down
newtime = New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, 30, Now.Second)
Label3.Text = newtime.ToShortTimeString
End If
ElseIf min > 45 And min <= 59 Then 'Fourth Quarter
If min > 52 Then
'move up
newtime = New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour + 1, 0, 0)
Label3.Text = newtime.ToShortTimeString
Else
'move down
newtime = New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, 45, Now.Second)
Label3.Text = newtime.ToShortTimeString
End IfEnd If newtime = Nothing min = Nothing End Sub
Recreating the wheel is the best way to appreciate what the previous coders have gone through to get you where you are now.