我想知道现在的时间是在早上还是晚上, 这是我的代码,但它不起作用。 我会很乐意为你提供帮助。
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
答案 0 :(得分:2)
你可以简化&#34;只需几小时就可以了:
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")
答案 1 :(得分:0)
你有:
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
对象= Now
将年,月,日设置为0001,01,01(使其无日历)。
使用日期时间比较器功能:
If DateTime.LessThan(mydatelesstime, eveningdate) And
DateTime.GreaterThanOrEqual(mydatelesstime, morningdate) Then
'I think its daytime
End If
试试。
答案 2 :(得分:0)
请务必在Form.Load事件上进行时间测试和标签更新。
我想检查一下Currntime.hour&gt; = 6&amp;&amp; Currntime.hour&lt; = 18.从0600到1800的时间通常被视为白天。除此之外是晚上。根据您的特殊品味进行调整,但不要比较对象,而是比较它们的属性。所以你最终会得到这样的东西:
If (DateAndTime.TimeOfDay.Hour >= 6 AndAlso DateAndTime.TimeOfDay.Hour <= 18) Then
Label2.text = "day"
Else
Label2.text = "night"
答案 3 :(得分:0)
跑步:
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