SolvedAddHours / Alarm

Posts 1–3 of 3 · Page 1 of 1
AddHours / Alarm
Hello,
I want help with a code that adds hours to the current time, i'll explain.
2 Buttons, 2 Labels:
First button makes Label1.text = timeofday and button 2 makes label2.text time of day + more 4 hours.
I always fail at the button 2 code, so can you help me? ty ^^
Where is the Date for Label1 coming from?

Scenario 1: Date object
Code:
Public Class Form1
    Private ReadOnly _date As DateTime = DateTime.Now

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Label1.Text = FormatDate(_date)
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Label2.Text = FormatDate(_date.AddHours(4))
    End Sub

    Private Shared Function FormatDate(ByVal dateObj As DateTime) As String
        Return String.Format("{0} {1}", dateObj.ToShortDateString(), dateObj.ToShortTimeString())
    End Function
End Class
Scenario 2: Parsing the date from the label
Code:
Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Label2.Text = FormatDate(DateTime.Parse(Label1.Text).AddHours(4))
    End Sub

    Private Shared Function FormatDate(ByVal dateObj As DateTime) As String
        Return String.Format("{0} {1}", dateObj.ToShortDateString(), dateObj.ToShortTimeString())
    End Function
End Class
Quote Originally Posted by Jason View Post
Where is the Date for Label1 coming from?

Scenario 1: Date object
Code:
Public Class Form1
    Private ReadOnly _date As DateTime = DateTime.Now

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Label1.Text = FormatDate(_date)
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Label2.Text = FormatDate(_date.AddHours(4))
    End Sub

    Private Shared Function FormatDate(ByVal dateObj As DateTime) As String
        Return String.Format("{0} {1}", dateObj.ToShortDateString(), dateObj.ToShortTimeString())
    End Function
End Class
Scenario 2: Parsing the date from the label
Code:
Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Label2.Text = FormatDate(DateTime.Parse(Label1.Text).AddHours(4))
    End Sub

    Private Shared Function FormatDate(ByVal dateObj As DateTime) As String
        Return String.Format("{0} {1}", dateObj.ToShortDateString(), dateObj.ToShortTimeString())
    End Function
End Class
Used the second method, thank you for helping.
Posts 1–3 of 3 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us