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

发布于 2025-01-14 04:30:17 字数 1753 浏览 1 评论 0原文

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

视觉此处的闹钟

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

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.

Visual of the alarm here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北座城市 2025-01-21 04:30:17

您的“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

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:

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
相思碎 2025-01-21 04:30:17

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

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

You could plus DateTime.Now with User Input Time.

 Dim UserTime As String()
 UserTime = Duration.Text.Split(':')
 AlarmTimeLBL.Text = DateTime.Now.AddHours(UserTime(0)).AddMinutes(UserTime(1)).AddSeconds(UserTime(2))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文