如何将 DateTimePicker 值添加到当前时间

How to add DateTimePicker value to Current Time

我遇到了困难。该程序是一个闹钟,它采用用户输入的持续时间并添加当前时间以获得闹钟结束时的未来时间标签。

我已经完成删除硬编码时间,但我无法将当前时间添加到持续时间

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

您可以加​​上 DateTime.Now 用户输入时间。

 Dim UserTime As String()
 UserTime = Duration.Text.Split(':')
 AlarmTimeLBL.Text = DateTime.Now.AddHours(UserTime(0)).AddMinutes(UserTime(1)).AddSeconds(UserTime(2))

您的“setTime”变量应该是 DateTime 以便它包含日期。这使得处理跨越午夜的当前时间和持续时间变得更加容易。

这是它想要的:

Private setTime As DateTime

Private Sub AlarmTimerFRM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    UpdateCurrentTime()
    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
    UpdateCurrentTime()
End Sub

Private Sub UpdateCurrentTime()
    outCurrentTimeLBL.Text = DateTime.Now.ToString("hh:mm:ss tt")
End Sub

Private Sub SetBTN_Click(sender As Object, e As EventArgs) Handles SetBTN.Click
    setTime = DateTime.Now.Add(setTimerDTP.Value.TimeOfDay)
    AlarmTimeLBL.Text = setTime.ToString("hh:mm:ss tt")
    SetBTN.Enabled = False
    setTimerDTP.Enabled = False
    Timer2.Start()
End Sub

Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
    If DateTime.Now >= setTime Then
        Timer2.Stop()
        MessageBox.Show("Alarm time reached!")
        SetBTN.Enabled = True
        setTimerDTP.Enabled = True
    End If
End Sub