vb.net

I need to find out if now is morning or night time


I would like to find out if the time now falls within the morning or night, Here is the code i have but it is not working. I will be great full for any assistance.

Dim ds as DateTime = #05:30:00 AM#
Dim de as DateTime = #05:30:00 PM#
Dim Currntime as Date = DateTime.Now.ToString("hh:mm:ss tt")
if Currntime <= ds AndAlso Currntime >= de then
Label2.text = "Morning"
Else
Label2.text = "Night"
End if

Here is what I have currently

If (TimeOfDay.Hour >= 6 AndAlso 
TimeOfDay.Hour <= 18) Then
Label2.text = "day"
Else
Label2.text = "night"
End If

Also doesn't work?

If (TimeOfDay.Hour >= 6 AndAlso TimeOfDay.Hour <= 18) Then
Label2.text = "day"
ElseIf (TimeOfDay.Hour >= 18 AndAlso TimeOfDay.Hour <= 6)
Label2.text = "night"
End If

Part where code is :

Imports System.Data.OleDb
Imports System.IO
Public Class Login
Inherits Form


Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Documents\ShiftHandOver.accdb;Persist Security Info=True;Jet OLEDB:Database")

Dim FindIDNow As String
Dim incc As Integer
Dim MaxRoww As Integer


Dim dss As New DataSet
Dim daa As OleDb.OleDbDataAdapter
Dim daae As OleDbDataAdapter
Dim sqll As String
Dim sqel As String

Dim dbProvider As String
Dim dbSource As String
Dim myDocuFold As String
Dim theDataBase As String
Dim FullDataBasePath As String
Dim timenow As Integer


Private Sub LoginForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    cmbPerm.Items.Clear()
    cmbPerm.Items.Add("Manager")
    cmbPerm.Items.Add("S-Manager")
    cmbPerm.Items.Add("Admin")
    cmbPerm.Items.Add("View")

    cmbShift.Items.Clear()
    cmbShift.Items.Add("Shift 1")
    cmbShift.Items.Add("Shift 2")
    cmbShift.Items.Add("Shift 3")
    cmbShift.Items.Add("Shift 4")
    conn.Open()
    sqll = "SELECT * FROM tblActions"
    daa = New OleDb.OleDbDataAdapter(sqll, conn)
    daa.Fill(dss, "tblActions")
    MaxRoww = dss.Tables("tblActions").Rows.Count

    incc = -1
    If incc <> 0 Then
        incc = 0

    End If
    conn.Close()
    If (TimeOfDay.Hour >= 6 AndAlso TimeOfDay.Hour <= 18) Then
        Label2.Text = "Morning"
    Else
        Label2.Text = "Night"
    End If





End Sub

Designer

lobal.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Login
Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        MyBase.Dispose(disposing)
    End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.  
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Login))
    Me.lblLogin = New System.Windows.Forms.Label()
    Me.Label1 = New System.Windows.Forms.Label()
    Me.cmbPerm = New System.Windows.Forms.ComboBox()
    Me.Cancel = New System.Windows.Forms.Button()
    Me.OK = New System.Windows.Forms.Button()
    Me.PasswordTextBox = New System.Windows.Forms.TextBox()
    Me.UsernameTextBox = New System.Windows.Forms.TextBox()
    Me.PasswordLabel = New System.Windows.Forms.Label()
    Me.UsernameLabel = New System.Windows.Forms.Label()
    Me.LogoPictureBox = New System.Windows.Forms.PictureBox()
    Me.Label3 = New System.Windows.Forms.Label()
    Me.cmbShift = New System.Windows.Forms.ComboBox()
    Me.Label2 = New System.Windows.Forms.Label()
    Me.Label4 = New System.Windows.Forms.Label()
    Me.Label5 = New System.Windows.Forms.Label()
    Me.Label6 = New System.Windows.Forms.Label()
    Me.Button1 = New System.Windows.Forms.Button()
    CType(Me.LogoPictureBox, System.ComponentModel.ISupportInitialize).BeginInit()
    Me.SuspendLayout()
    '
    'lblLogin
    '
    Me.lblLogin.AutoSize = True
    Me.lblLogin.Location = New System.Drawing.Point(272, 104)
    Me.lblLogin.Name = "lblLogin"
    Me.lblLogin.Size = New System.Drawing.Size(22, 13)
    Me.lblLogin.TabIndex = 28
    Me.lblLogin.Text = "....."
    '
    'Label1
    '
    Me.Label1.Location = New System.Drawing.Point(203, 99)
    Me.Label1.Name = "Label1"
    Me.Label1.Size = New System.Drawing.Size(58, 23)
    Me.Label1.TabIndex = 26
    Me.Label1.Text = "&Permission"
    Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
    '
    'cmbPerm
    '
    Me.cmbPerm.FormattingEnabled = True
    Me.cmbPerm.Location = New System.Drawing.Point(203, 125)
    Me.cmbPerm.Name = "cmbPerm"
    Me.cmbPerm.Size = New System.Drawing.Size(216, 21)
    Me.cmbPerm.TabIndex = 24
    '
    'Cancel
    '
    Me.Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
    Me.Cancel.Location = New System.Drawing.Point(325, 211)
    Me.Cancel.Name = "Cancel"
    Me.Cancel.Size = New System.Drawing.Size(94, 23)
    Me.Cancel.TabIndex = 27
    Me.Cancel.Text = "&Cancel"
    '
    'OK
    '
    Me.OK.Location = New System.Drawing.Point(203, 211)
    Me.OK.Name = "OK"
    Me.OK.Size = New System.Drawing.Size(94, 23)
    Me.OK.TabIndex = 25
    Me.OK.Text = "&OK"
    '
    'PasswordTextBox
    '
    Me.PasswordTextBox.Location = New System.Drawing.Point(203, 75)
    Me.PasswordTextBox.Name = "PasswordTextBox"
    Me.PasswordTextBox.PasswordChar = Global.Microsoft.VisualBasic.ChrW(42)
    Me.PasswordTextBox.Size = New System.Drawing.Size(216, 20)
    Me.PasswordTextBox.TabIndex = 23
    '
    'UsernameTextBox
    '
    Me.UsernameTextBox.Location = New System.Drawing.Point(203, 29)
    Me.UsernameTextBox.Name = "UsernameTextBox"
    Me.UsernameTextBox.Size = New System.Drawing.Size(216, 20)
    Me.UsernameTextBox.TabIndex = 21
    '
    'PasswordLabel
    '
    Me.PasswordLabel.Location = New System.Drawing.Point(203, 55)
    Me.PasswordLabel.Name = "PasswordLabel"
    Me.PasswordLabel.Size = New System.Drawing.Size(216, 21)
    Me.PasswordLabel.TabIndex = 22
    Me.PasswordLabel.Text = "&Password"
    Me.PasswordLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
    '
    'UsernameLabel
    '
    Me.UsernameLabel.Location = New System.Drawing.Point(203, 9)
    Me.UsernameLabel.Name = "UsernameLabel"
    Me.UsernameLabel.Size = New System.Drawing.Size(216, 21)
    Me.UsernameLabel.TabIndex = 19
    Me.UsernameLabel.Text = "&User name"
    Me.UsernameLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
    '
    'LogoPictureBox
    '
    Me.LogoPictureBox.Dock = System.Windows.Forms.DockStyle.Left
    Me.LogoPictureBox.Image = CType(resources.GetObject("LogoPictureBox.Image"), System.Drawing.Image)
    Me.LogoPictureBox.Location = New System.Drawing.Point(0, 0)
    Me.LogoPictureBox.Name = "LogoPictureBox"
    Me.LogoPictureBox.Size = New System.Drawing.Size(165, 317)
    Me.LogoPictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom
    Me.LogoPictureBox.TabIndex = 20
    Me.LogoPictureBox.TabStop = False
    '
    'Label3
    '
    Me.Label3.Location = New System.Drawing.Point(203, 146)
    Me.Label3.Name = "Label3"
    Me.Label3.Size = New System.Drawing.Size(58, 23)
    Me.Label3.TabIndex = 30
    Me.Label3.Text = "&Shift"
    Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
    '
    'cmbShift
    '
    Me.cmbShift.FormattingEnabled = True
    Me.cmbShift.Location = New System.Drawing.Point(203, 172)
    Me.cmbShift.Name = "cmbShift"
    Me.cmbShift.Size = New System.Drawing.Size(216, 21)
    Me.cmbShift.TabIndex = 29
    '
    'Label2
    '
    Me.Label2.AutoSize = True
    Me.Label2.Location = New System.Drawing.Point(380, 9)
    Me.Label2.Name = "Label2"
    Me.Label2.Size = New System.Drawing.Size(39, 13)
    Me.Label2.TabIndex = 31
    Me.Label2.Text = "Label2"
    '
    'Label4
    '
    Me.Label4.AutoSize = True
    Me.Label4.Location = New System.Drawing.Point(200, 246)
    Me.Label4.Name = "Label4"
    Me.Label4.Size = New System.Drawing.Size(39, 13)
    Me.Label4.TabIndex = 32
    Me.Label4.Text = "Label4"
    '
    'Label5
    '
    Me.Label5.AutoSize = True
    Me.Label5.Location = New System.Drawing.Point(200, 268)
    Me.Label5.Name = "Label5"
    Me.Label5.Size = New System.Drawing.Size(39, 13)
    Me.Label5.TabIndex = 33
    Me.Label5.Text = "Label5"
    '
    'Label6
    '
    Me.Label6.AutoSize = True
    Me.Label6.Location = New System.Drawing.Point(200, 281)
    Me.Label6.Name = "Label6"
    Me.Label6.Size = New System.Drawing.Size(39, 13)
    Me.Label6.TabIndex = 34
    Me.Label6.Text = "Label6"
    '
    'Button1
    '
    Me.Button1.Location = New System.Drawing.Point(383, 258)
    Me.Button1.Name = "Button1"
    Me.Button1.Size = New System.Drawing.Size(75, 23)
    Me.Button1.TabIndex = 35
    Me.Button1.Text = "Button1"
    Me.Button1.UseVisualStyleBackColor = True
    '
    'Login
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.ClientSize = New System.Drawing.Size(500, 317)
    Me.Controls.Add(Me.Button1)
    Me.Controls.Add(Me.Label6)
    Me.Controls.Add(Me.Label5)
    Me.Controls.Add(Me.Label4)
    Me.Controls.Add(Me.Label2)
    Me.Controls.Add(Me.Label3)
    Me.Controls.Add(Me.cmbShift)
    Me.Controls.Add(Me.lblLogin)
    Me.Controls.Add(Me.Label1)
    Me.Controls.Add(Me.cmbPerm)
    Me.Controls.Add(Me.Cancel)
    Me.Controls.Add(Me.OK)
    Me.Controls.Add(Me.PasswordTextBox)
    Me.Controls.Add(Me.UsernameTextBox)
    Me.Controls.Add(Me.PasswordLabel)
    Me.Controls.Add(Me.UsernameLabel)
    Me.Controls.Add(Me.LogoPictureBox)
    Me.Name = "Login"
    Me.Text = "Login"
    CType(Me.LogoPictureBox, System.ComponentModel.ISupportInitialize).EndInit()
    Me.ResumeLayout(False)
    Me.PerformLayout()

End Sub

Friend WithEvents lblLogin As Label
Friend WithEvents Label1 As Label
Friend WithEvents cmbPerm As ComboBox
Friend WithEvents Cancel As Button
Friend WithEvents OK As Button
Friend WithEvents PasswordTextBox As TextBox
Friend WithEvents UsernameTextBox As TextBox
Friend WithEvents PasswordLabel As Label
Friend WithEvents UsernameLabel As Label
Friend WithEvents LogoPictureBox As PictureBox
Friend WithEvents Label3 As Label
Friend WithEvents cmbShift As ComboBox
Friend WithEvents Label2 As Label
Friend WithEvents Label4 As Label
Friend WithEvents Label5 As Label
Friend WithEvents Label6 As Label
Friend WithEvents Button1 As Button
End Class

Solution

  • For Running:

    If (TimeOfDay.Hour >= 6 AndAlso TimeOfDay.Hour <= 18) Then
            Label2.Text = "Morning"
        Else
            Label2.Text = "Night"
        End If
    

    For Testing:

    Dim time =#05:30 AM#.TimeOfDay
        If (time.Hours >= 6 AndAlso time.Hours <= 18) Then
            Label2.Text = "Morning"
        Else
            Label2.Text = "Night"
        End If