【问题标题】:I need to find out if now is morning or night time我需要知道现在是早上还是晚上
【发布时间】:2017-10-31 09:18:40
【问题描述】:

我想知道现在时间是在早上还是晚上, 这是我拥有的代码,但它不起作用。 我会很乐意提供任何帮助。

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

这是我目前拥有的

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

也不行?

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

代码所在的部分:

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

设计师

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

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    你有:

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

    应该是:

    Dim Currntime as Date = DateTime.Now
    

    我很惊讶你没有收到InvalidCastException

    其次,您初始化的日期是 01/01/0001 05:30:00,这意味着任何 Now DateTime 对象总是会更大。你明白我在说什么吗?

    我要做的是创建一个新的DateTime Object = Now

    将年、月、日设置为 0001、01、01(使其无日期)。

    使用日期时间比较函数:

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

    试试看。

    【讨论】:

    • 谢谢,做出了改变,但仍然只显示“夜晚”?
    • 更新了我的答案
    • 为什么要使用静态LessThanGreaterThanOrEqual 方法而不是仅仅使用&lt;&gt;= 运算符以获得更好的可读性?另外,我建议您阅读difference between And and AndAlso, Or and OrElse
    • 我并没有说你这样做的方式有什么问题,只是它可能有点难以阅读和理解。
    • 旧习难改;)
    【解决方案2】:

    请务必对 Form.Load 事件进行时间测试和标签更新。

    我喜欢检查 Currntime.hour >= 6 && Currntime.hour

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

    【讨论】:

    • 改成VB了,没注意到tag,不好意思。
    • @RGS 谢谢,没有定义类型'DateTime.now'?
    • Now 是一个静态/共享属性 - 你不能New与它一起使用
    • @Plutonix 有一段时间了,抱歉。正确的代码现已发布。
    • @RGS 感谢仍然没有得到正确的结果,无论如何都只表示“晚上”。
    【解决方案3】:

    您可以通过仅获取小时数来“简化”它:

    Dim hours = Now.TimeOfDay.TotalHours     ' or Dim hours = #5:3PM#.TimeOfDay.TotalHours 
    
    Label2.Text = If(hours <= 5.5 OrElse hours >= 17.5, "Morning", "Night")
    

    或者更短一点:

    Label2.Text = If(Now.AddHours(-5.5).TimeOfDay.Hours >= 12, "Morning", "Night")
    

    【讨论】:

    • 完美满足我的需要,谢谢
    【解决方案4】:

    跑步:

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

    用于测试:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-08
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      相关资源
      最近更新 更多