[RESOLVED] How to get a TimeZone, its UTC offset in TimeSpan format?
Hi, Consider you have a clock control which works on GMT and has a UTC offset parameter in shh:mm:ss (System.TimeSpan) format. You also have a Combobox1 which contains all possible timezones via
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange(TimeZoneInfo.GetSystemTimeZones.ToArray)
End Sub
The goal is when you change the ComboBox1 index/item clock automatically converts the time.
Please note that Combobox may return something like "(UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna" but we only require its +01:00:00 (TimSpan).
(I am ParseExact and FormatProvider noob, sorry)
Sincerely,
Pourkascheff/.
Re: How to get a TimeZone, its UTC offset in TimeSpan format?
Why not just use the Offset itself?
https://learn.microsoft.com/en-us/do...-baseutcoffset
Why parse the crap out of a String?
Re: How to get a TimeZone, its UTC offset in TimeSpan format?
Quote:
Originally Posted by
Zvoni
Why not just use the Offset itself?
Not sure how to do it. Can you hint a clue as a code example? I found learn.microsoft articles confusing... :'(
Quote:
Originally Posted by
Zvoni
Why parse the crap out of a String?
Maybe the last solution?
Re: How to get a TimeZone, its UTC offset in TimeSpan format?
It's just a property. You get it like any other property. It's already a TimeSpan.
1 Attachment(s)
Re: How to get a TimeZone, its UTC offset in TimeSpan format?
This is an obvious format mismatch/incompatibility. Can you say your point in another words?
Attachment 186262
Re: How to get a TimeZone, its UTC offset in TimeSpan format?
Code:
Clock1.UtcOffset = DirectCast(ComboBox1.SelectedItem, TimeZoneInfo).BaseUtcOffset
Re: How to get a TimeZone, its UTC offset in TimeSpan format?
Fantastic, it worked properly\!
Re: How to get a TimeZone, its UTC offset in TimeSpan format?
Quote:
Originally Posted by
pourkascheff
Fantastic, it worked properly\!
You loaded TimeZoneInfos into your ComboBox, but the Items are always retrieved as Type Object. You need to cast as TimeZoneInfo so you can access the utc offset property… That’s why it worked…