Kraxed
Kraxed

Reputation: 350

TimeOfDay giving incorrect time

I made a timer which refreshes every 1000 milliseconds and I put the code as:

Private Sub RealTimeTMR_Tick(sender As Object, e As EventArgs) Handles RealTimeTMR.Tick
    TimeLBL.Text = TimeOfDay.ToLocalTime.ToString
End Sub

One would suspect this to be flawless and simple but when I start the program I get the value of the label as: 01/01/0001 18:59:36

If it make any difference to formats or anything I live in Ireland.

Upvotes: 0

Views: 839

Answers (2)

Jim Mischel
Jim Mischel

Reputation: 134005

If you only want the time, use:

TimeLBL.Text = DateTime.Now.TimeOfDay.ToString("g")

See Standard TimeSpan Format Strings and Custom TimeSpan Format Strings for more information about formatting.

Upvotes: 3

nkvu
nkvu

Reputation: 5841

It's because TimeOfDay only does the time component and has its Date component set to all 1's.

You could use something like DateTime.Now to get this information - it should return the current local date and time of the machine it's executed on.

Upvotes: 3

Related Questions