如何将 DateTimePicker 值添加到当前时间
我对此感到很困难。该程序是一个闹钟,它接受用户输入的持续时间并添加当前时间以获得闹钟结束时的未来时间的标签。
我已完成删除硬编码的时间,但无法将当前时间添加到持续时间中
Option Strict On
Public Class AlarmTimerFRM
Private setTime As Date
Private Sub AlarmTimerFRM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
outCurrentTimeLBL.Text = Date.Now.ToLongTimeString
outCurrentTimeLBL.BackColor = Color.LightGray
Timer1.Start()
setTimerDTP.Format = DateTimePickerFormat.Custom
setTimerDTP.CustomFormat = "HH:mm:ss"
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
outCurrentTimeLBL.Text = TimeOfDay.ToString("hh:mm:ss tt")
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
outCurrentTimeLBL.Text = TimeOfDay.ToString("hh:mm:ss tt")
End Sub
Private Sub SetBTN_Click(sender As Object, e As EventArgs) Handles SetBTN.Click
REM sets the alarm time
Dim OneHourAgo As DateTime
Dim FormattedTime As String
OneHourAgo = Now.AddHours(-1)
FormattedTime = OneHourAgo.ToString("HH:mm:ss")
AlarmTimeLBL.Text = (FormattedTime)
End Sub
Private Sub setTimerDTP_ValueChanged(sender As Object, e As EventArgs) Handles setTimerDTP.ValueChanged
End Sub
Private Sub ResetBTN_Click(sender As Object, e As EventArgs) Handles ResetBTN.Click
REM Resets the alarm time LBL
AlarmTimeLBL.Text = ""
End Sub
Private Sub AlarmTimeLBL_Click(sender As Object, e As EventArgs) Handles AlarmTimeLBL.Click
End Sub
End Class
I'm having a difficult time with this. The program is an Alarm clock that takes a user input duration of time and adds the current time to get a label of the future time of when the alarm is finished.
I have accomplished removing hardcoded hours but I can't add the current time to the duration
Option Strict On
Public Class AlarmTimerFRM
Private setTime As Date
Private Sub AlarmTimerFRM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
outCurrentTimeLBL.Text = Date.Now.ToLongTimeString
outCurrentTimeLBL.BackColor = Color.LightGray
Timer1.Start()
setTimerDTP.Format = DateTimePickerFormat.Custom
setTimerDTP.CustomFormat = "HH:mm:ss"
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
outCurrentTimeLBL.Text = TimeOfDay.ToString("hh:mm:ss tt")
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
outCurrentTimeLBL.Text = TimeOfDay.ToString("hh:mm:ss tt")
End Sub
Private Sub SetBTN_Click(sender As Object, e As EventArgs) Handles SetBTN.Click
REM sets the alarm time
Dim OneHourAgo As DateTime
Dim FormattedTime As String
OneHourAgo = Now.AddHours(-1)
FormattedTime = OneHourAgo.ToString("HH:mm:ss")
AlarmTimeLBL.Text = (FormattedTime)
End Sub
Private Sub setTimerDTP_ValueChanged(sender As Object, e As EventArgs) Handles setTimerDTP.ValueChanged
End Sub
Private Sub ResetBTN_Click(sender As Object, e As EventArgs) Handles ResetBTN.Click
REM Resets the alarm time LBL
AlarmTimeLBL.Text = ""
End Sub
Private Sub AlarmTimeLBL_Click(sender As Object, e As EventArgs) Handles AlarmTimeLBL.Click
End Sub
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的“setTime”变量应该是一个
DateTime
,以便它包含日期。这使得处理当前时间和跨越午夜的持续时间变得更加容易。这就是它想要的:
Your "setTime" variable should be a
DateTime
so that it includes the date. This makes it easier to deal with current times and durations that span midnight.Here's what it would like:
您可以加上 DateTime.Now 和用户输入时间。
You could plus DateTime.Now with User Input Time.