|
-
Nov 18th, 2022, 06:24 AM
#1
Thread Starter
Hyperactive Member
[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/.
-
Nov 18th, 2022, 06:41 AM
#2
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?
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Nov 18th, 2022, 06:44 AM
#3
Thread Starter
Hyperactive Member
Re: How to get a TimeZone, its UTC offset in TimeSpan format?
 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... :'(
 Originally Posted by Zvoni
Why parse the crap out of a String?
Maybe the last solution?
-
Nov 18th, 2022, 07:34 AM
#4
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.
-
Nov 18th, 2022, 09:56 AM
#5
Thread Starter
Hyperactive Member
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?
-
Nov 18th, 2022, 03:51 PM
#6
Re: How to get a TimeZone, its UTC offset in TimeSpan format?
Code:
Clock1.UtcOffset = DirectCast(ComboBox1.SelectedItem, TimeZoneInfo).BaseUtcOffset
Last edited by .paul.; Nov 18th, 2022 at 03:55 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 19th, 2022, 12:26 AM
#7
Thread Starter
Hyperactive Member
Re: How to get a TimeZone, its UTC offset in TimeSpan format?
Fantastic, it worked properly\!
-
Nov 19th, 2022, 01:53 AM
#8
Re: How to get a TimeZone, its UTC offset in TimeSpan format?
 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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|