Click here to Skip to main content
15,905,068 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to check the current time with the half an hour time slots (total of 48 starts from 00:00,00:30,01:00,....23:30). Is it possible using select case statement. If not any other suggestions please?

I am storing the current time in an object. now i need to do conditional check on this variable.

Dim datime As DateTime = DateTime.Now.ToString("t")
Posted
Comments
[no name] 4-Jul-11 13:21pm    
Do you want to do something different each half hour?
Member 8001800 4-Jul-11 13:34pm    
Into whichever the time slot the current time goes, I need to write a value under corresponding time slot

The problem is, you don't want, and can't count on, an exact time. You're better off with

if (datime.TotalMinutes <= 30)
{
}
else if (datime.TotalMinutes <= 60)
{
}

etc, instead of a switch statement, which would require you entering every possible value. Note I used 'TotalMinutes' if you used Minute, it would only go up to 59.
 
Share this answer
 
You need to implement timer[^], which provides a mechanism for executing a method at specified intervals.

--EDIT1---
OK, create new windows application project and place label on the form. Then copy and paste code below:
VB
Public Class Form1
    Delegate Sub SetTextCallback(ByVal [text] As String)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Create an inferred delegate that invokes methods for the timer.
        Dim tcb As TimerCallback = AddressOf CheckSendReport
        Dim oTimer As System.Threading.Timer = New System.Threading.Timer(tcb, 0, 0, 60000)
    End Sub
    
    'This method is called by the timer delegate.
    'Every (period time - in example 60000) we call DrawMessage sub
    Sub CheckSendReport(ByVal state As Object)
        Dim dTime As Date = Date.Now
        If dTime.Minute = 30 Then
            DrawMessage(dTime.ToString & " - Now!")
        Else
            DrawMessage(dTime.ToString & " - Not yet!")
        End If
    End Sub

    'draw message
    Private Sub DrawMessage(ByVal sMsg As String)
        If Me.Label1.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf DrawMessage)
            Me.Invoke(d, New Object() {sMsg})
        Else
            Me.Label1.Text = sMsg
        End If
    End Sub
End Class


I hope it's helpful.
 
Share this answer
 
v3
Comments
Member 8001800 4-Jul-11 14:03pm    
In my question, the time i am storing in a avariable is nothing but the timer execution time. If timer executing for every 10 mins, (example: 10:10,10:20,10:30) the values for all three times should be written under 10:30 slot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900