0

It's a simple clock

My Timer1_Tick get that code:

LocalTime.Text = TimeOfDay.ToString("h:mm:ss tt")

How to add 6 hours to it? Thank you

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Aurel_VB
  • 31
  • 1
  • 6

2 Answers2

3

You should not use labels and textboxes to store your data (a time in this case). Labels should only be used to display some information and textboxes to display and enter information. Store your data in variables, fields and properties.

Define this field in the form

Dim t As Date = Date.Now

In the method

t = t.AddHours(6)
LocalTime.Text = t.ToString("h:mm:ss tt")

I.e. you always work with the value stored in the field and then update the label text from it.

Since the time in the label is stored as a string, you cannot add hours easily. You would have to convert it back to a Date structure, add the hours and then convert it back to a string.


If you want to display several clocks in the Timer_Tick you can do this (note that Date in VB is just an alias for the System.DateTime structure):

Dim local = DateTime.Now
LocalTime.Text = local.ToString("h:mm:ss tt")
AnotherLabel.Text = local.AddHours(6).ToString("h:mm:ss tt")
YetAnotherLabel.Text = local.AddHours(-2).ToString("h:mm:ss tt")
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

DateTime.TimeOfDay is a TimeSpan. Thus, you could use Hours property or Add method to change the value. For example:

LocalTime.Text = TimeOfDay.Add(TimeSpan.FromHours(6)).ToString("hh\:mm\:ss")

To subtract hours use Subtract method instead of Add. Also, the same result could be obtained by using Add method with negative TimeSpan values:

LocalTime.Text = TimeOfDay.Add(TimeSpan.FromHours(-6)).ToString("hh\:mm\:ss")

Note, a TimeSpanrepresents a time interval. Let, there is value of elapsed time that is equal to 56 hours 36 minutes and 12 seconds. The AM/PM mark isn't actual for this value. Thus, to get AM/PM time format you need to use values of DateTime structure instead of TimeSpan:

NEWYORK.Text = DateTime.Now.Add(TimeSpan.FromHours(6)).ToString("hh:mm tt")

See details in the Choosing between DateTime, DateTimeOffset, TimeSpan, and TimeZoneInfo article.

Alexander
  • 4,420
  • 7
  • 27
  • 42
  • to substract i have to put - Sub ? – Aurel_VB Sep 02 '17 at 17:28
  • Thank you Olivier, but the code of Alexander is quite easy and simple for my level :-) Also, i can't substract hours and get AM or PM with that code why? LocalTime.Text = TimeOfDay ActualDate.Text = Date.Now.ToString("ddd MM yyyy") NEWYORK.Text = TimeOfDay.Add(TimeSpan.FromHours(6)).ToString("hh\:mm tt") LOSANGELES.Text = TimeOfDay.Add(TimeSpan.FromHours(9)).ToString("hh\:mm")' – Aurel_VB Sep 02 '17 at 17:58
  • @Aurel_VB , to substract use negative timespan value. In your case use `-TimeSpan.FromHours(6)` or `TimeSpan.FromHours(-6)` – Alexander Sep 03 '17 at 03:12
  • @Aurel_VB , `TimeSpan` represents a **time interval**. This could be greater than 24 hours. Thus, to use AM/PM time format you need to use values of [`DateTime`](https://msdn.microsoft.com/ru-ru/library/system.datetime(v=vs.110).aspx) structure instead of `TimeSpan`, for example: `NEWYORK.Text = DateTime.Now.Add(TimeSpan.FromHours(6)).ToString("hh:mm tt")`. – Alexander Sep 03 '17 at 04:53
  • thank you but i don't know why, AM PM does not appear... :-o I'm getting nervous to do not understand why it doesn't work... :-\ I am using DateTime.UtcNow – Aurel_VB Sep 03 '17 at 07:58
  • @Aurel_VB , the [`tt`](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#ttSpecifier) format specifier sould help you. – Alexander Sep 03 '17 at 08:17
  • i am so sorry about that but - tt - don't want show... PARIS.Text = local.AddHours(2).ToString("HH:mm tt") – Aurel_VB Sep 03 '17 at 09:22
  • Got it! My windows system set in France of course, so couldn't see it :-) – Aurel_VB Sep 03 '17 at 10:14
  • @Aurel_VB , you could use `InvariantCulture` option to ignore system localization settings (see [Andy's answer](https://stackoverflow.com/a/7875351/7914637) for details). – Alexander Sep 03 '17 at 10:43
  • How to use it? Just before form declaration? – Aurel_VB Sep 03 '17 at 13:34
  • @Aurel_VB , there's second argument of `ToString` method: `.ToString("hh:mm tt", System.Globalization.CultureInfo.InvariantCulture)` – Alexander Sep 03 '17 at 14:12