0

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
Jj84
  • 17
  • 7

4 Answers4

2

You can "simplify" it a bit by getting just the hours:

Dim hours = Now.TimeOfDay.TotalHours     ' or Dim hours = #5:3PM#.TimeOfDay.TotalHours 

Label2.Text = If(hours <= 5.5 OrElse hours >= 17.5, "Morning", "Night")

or a bit shorter:

Label2.Text = If(Now.AddHours(-5.5).TimeOfDay.Hours >= 12, "Morning", "Night")
Slai
  • 22,144
  • 5
  • 45
  • 53
0

You have:

Dim Currntime as Date = DateTime.Now.ToString("hh:mm:ss tt")

Should be:

Dim Currntime as Date = DateTime.Now

I am surprised you didn't get an InvalidCastException

Secondly, your date that you initialise is 01/01/0001 05:30:00, this means any Now DateTime object will ALWAYS be greater. Do you see what I am getting at?

What I would do is create a new DateTime Object = Now

Set the year, month and day to 0001, 01, 01 (make it dateless).

Use the datetime comparitor function:

If DateTime.LessThan(mydatelesstime, eveningdate) And
   DateTime.GreaterThanOrEqual(mydatelesstime, morningdate) Then
       'I think its daytime
End If

Try that.

Mike
  • 419
  • 4
  • 11
  • Thank you, Made the change but it remains to only indicate "Night"? – Jj84 May 30 '17 at 19:14
  • Updated my answer – Mike May 30 '17 at 19:23
  • Updated the answer again. – Mike May 30 '17 at 19:39
  • Rather than setting the properties of the DateTime after creation, simply set it at creation: `Dim datelesstime as New DateTime(1, 1, 1, Now.Hours, Now.Minutes, Now.Seconds)` – Mike May 30 '17 at 19:51
  • 1
    Why are you using the static `LessThan` and `GreaterThanOrEqual` methods instead of just using the `<`, `>=` operators for better readability? Also, I recommend you read about the [**difference between `And` and `AndAlso`, `Or` and `OrElse`**](https://stackoverflow.com/questions/8409467/orelse-and-or-and-andalso-and-and-when-to-use). – Visual Vincent May 30 '17 at 20:21
  • @VisualVincent - Why not? I am not a fan of using numeric and/or logic operators for non-generic types, in the same way I don't like to use <, >, etc for string comparisons. Maybe this is a conversation best suited to having with the Microsoft team for including those methods in the first place. – Mike May 30 '17 at 20:29
  • `<` and `>` is very unusual to use on strings, but not as much for `DateTime` objects actually. [**Operators can be overridden/defined**](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/how-to-define-an-operator), which is the case for `DateTime`. Infact those methods are [**exactly the same used for the operators**](https://msdn.microsoft.com/en-us/library/system.datetime.op_greaterthanorequal(v=vs.95).aspx). – Visual Vincent May 30 '17 at 20:34
  • If an operator isn't defined IntelliSense will give you an error. So if an operator works for a non-primitive type then you know that the operator has been specifically defined for that type (whether it is correctly implemented however is a different matter, but I think you can trust Microsoft ;). – Visual Vincent May 30 '17 at 20:37
  • 1
    I didn't say there was anything wrong with the way you do it, only that it is perhaps a bit harder to read and understand. – Visual Vincent May 30 '17 at 20:41
  • 1
    Old habits are hard to break ;) – Mike May 30 '17 at 20:44
0

Please be sure to do your time test and label update on the Form.Load event.

I like to check if Currntime.hour >= 6 && Currntime.hour <= 18. The time from 0600 to 1800 is typically regarded as daytime. Outside of that is night. Adjust for your particular taste, but don't compare the Objects, compare their properties instead. So you would end up with something like this:

If (DateAndTime.TimeOfDay.Hour >= 6 AndAlso DateAndTime.TimeOfDay.Hour <= 18) Then
Label2.text = "day"
Else
Label2.text = "night"
RGS
  • 77
  • 2
  • 9
  • Changed to VB, didn't notice the tag, sorry. – RGS May 30 '17 at 20:23
  • @RGS Thanks , Type 'DateTime.now' is not defined? – Jj84 May 30 '17 at 20:36
  • `Now` is a static/shared property - you *cant* use `New` with it – Ňɏssa Pøngjǣrdenlarp May 30 '17 at 20:38
  • @Plutonix Been a while, sorry about that. The correct code is now posted. – RGS May 30 '17 at 20:50
  • @RGS thanks still not getting the correct result, only indicating "night" regardless. – Jj84 May 30 '17 at 20:57
  • Are you comparing the Object instead of its properties? Object comparison, like what you had, will always give you the else clause because the two Objects are different. If you are comparing just the hour value of the TimeOfDay property got your if/else is correct then it will work. Post your full code now after your latest revision please so I can see what you have. – RGS May 30 '17 at 21:11
  • Try Clean and Rebuild. You have it correct. Maybe close and relaunch visual studio, also you can try it out on .NET Fiddle – RGS May 30 '17 at 21:38
  • I did all that, I took it and ran in a stand alone form where just this is and it still only indicate Night???? Any ideas? – Jj84 May 30 '17 at 21:55
  • Yes. Did you perform the time test and label update on Form.Load event ? – RGS May 30 '17 at 21:57
  • Yes, but still same – Jj84 May 30 '17 at 22:06
  • Please post the full code, everything in the Class. – RGS May 30 '17 at 22:08
  • And everything in the Designer file – RGS May 30 '17 at 22:18
  • I don't see your Designer file, but I'd hazard a guess that in it the Label which you believe should be named Label2 is actually not that name where it is defined. Also, the Label in question: That label lives in this Class, correct? The Designer file would help a lot. – RGS May 30 '17 at 22:34
  • Oh, you have 18 and 6 backwards. Should be greater than or equal to 6, and less than or equal to 18. – RGS May 30 '17 at 22:43
  • I'm aware, I'm testing it that way as it is 12:45 AM here so would automatically default to night. Only managed to upload image as I'm working on 2 different computers – Jj84 May 30 '17 at 22:46
  • Oh I need to see the Designer code, not screenshot. So, it would be FormName.Designer.vb Need to see that. https://stackoverflow.com/questions/7099694/view-designer-code-in-visual-studio-2010 – RGS May 30 '17 at 22:50
  • I added the designer form – Jj84 May 30 '17 at 22:57
  • Look at your load Method versus the Class name, they're different indicating something was improperly renamed / refactored at some point. Check the Event bindings for that form, make sure the Load event goes to that LoginForm1_Load instead of Login_Load.... – RGS May 30 '17 at 23:03
  • @Jj84 .Hour can't be more than 18 and less than 8 at the same time, so that will always be False – Slai May 30 '17 at 23:12
  • @RGS checked, what did you mean by performing time test and label update? – Jj84 May 30 '17 at 23:13
  • @Slai ok, how do I go about it then? what would the If statement be then? Or what Would you Use? – Jj84 May 30 '17 at 23:15
  • @Jj84 you can for example set the time manually to test `Dim time = #7:23PM#.TimeOfDay` – Slai May 30 '17 at 23:19
  • @RGS, thank you for your assistance, working now and thank you for your patients – Jj84 May 30 '17 at 23:30
0

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
Jj84
  • 17
  • 7