XK8ER
XK8ER

Reputation: 780

Setup a count down based on a time

I would like to know how to setup a count down based on a time.. for example its 6pm and there is a time like 11PM, how would I have a countdown that I click a button no timer, that shows me you have 5 hours left etc.

Dim myTime As String = "7AM"
Dim date1 As DateTime = Date.Now
Dim date2 As DateTime = Convert.ToDateTime(Date.Now.Date.ToLongDateString & " " & myTime)
Dim ts As New TimeSpan
ts = date2 - date1
TextBox.Text = ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds

Upvotes: 0

Views: 69

Answers (1)

Codemunkeee
Codemunkeee

Reputation: 1613

If I got it right, do you want to get the remaining time by clicking a button? which also means that you have to click it everytime for it to update?

Dim myTime As String = "7AM"
Dim date1 As DateTime = System.DateTime.Now.ToString("hh:mm:ss tt, dd MMMM yyyy")
Dim date2 As DateTime = Convert.ToDateTime(Date.Now.Date.ToLongDateString & " " & myTime)

Dim ts As New TimeSpan
If date2 < date1 Then
    date2 = date2.AddDays(1)
End If
ts = date2 - date1


MsgBox(ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds)

That would return a time with 16:30:00 with hh:mm:ss format.

Upvotes: 4

Related Questions