TimeOfDay adds 1/1/0001 to the Time value?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I have a label in my app where I display time from a Timer contol like this:

Private Sub Timer1_Tick(...) Handles Timer1.Tick
lbl_Time.Text = TimeOfDay.ToString
End Sub

The display is 1/1/0001 10:00:01 AM

How can I remove the 1/1/0001 part?

Thanks,
Rich
 
Hmmm, I tried

TimeOfDay.ToShortTime.ToString

which seems to display just the time value 10:07 AM but no seconds. Any
suggestions appreciated how I can display a time value in this form:

10:07:23 AM
 
Rich said:
Hello,

I have a label in my app where I display time from a Timer contol
like this:

Private Sub Timer1_Tick(...) Handles Timer1.Tick
lbl_Time.Text = TimeOfDay.ToString
End Sub

The display is 1/1/0001 10:00:01 AM

How can I remove the 1/1/0001 part?


ToString is overloaded. Use one of the other versions.

Also consider using Date.TimeOfDay. Returns a TimeSpan object with no
formatting features with the ToString method, but doesn't contain a Date
part.


Armin
 
How about this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Label1.Text = TimeOfDay.ToString("hh:mm:ss tt")

End Sub

That will give the current time showing only AM/PM instead of the date.
(modify to work with your timer)
james
 
Or even better: (sorry for answering my own response)

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Timer1.Tick

Label1.Text = TimeOfDay.ToString("hh:mm:ss tt")

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load

Timer1.Interval = 100

Timer1.Enabled = True

End Sub


james
 
Back
Top