Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Round to nearest quarter of hour

Round to nearest quarter of hour

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VK Cadec
    wrote on last edited by
    #1

    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.

    U 1 Reply Last reply
    0
    • V VK Cadec

      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.

      U Offline
      U Offline
      Uber1
      wrote on last edited by
      #2

      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.

      U 1 Reply Last reply
      0
      • U Uber1

        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.

        U Offline
        U Offline
        Uber1
        wrote on last edited by
        #3

        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 If

            End 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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups