TimeOfDay giving incorrect time

836 Views Asked by At

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.

2

There are 2 best solutions below

0
nkvu On BEST ANSWER

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.

1
Jim Mischel On

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.