Results 1 to 8 of 8

Thread: [RESOLVED] How to get a TimeZone, its UTC offset in TimeSpan format?

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Resolved [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/.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    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

  3. #3

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: How to get a TimeZone, its UTC offset in TimeSpan format?

    Quote Originally Posted by Zvoni View Post
    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 View Post
    Why parse the crap out of a String?
    Maybe the last solution?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    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?
    Name:  timezone error.jpg
Views: 785
Size:  13.0 KB

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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.

  7. #7

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: How to get a TimeZone, its UTC offset in TimeSpan format?

    Fantastic, it worked properly\!

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: How to get a TimeZone, its UTC offset in TimeSpan format?

    Quote Originally Posted by pourkascheff View Post
    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…

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
  •  



Click Here to Expand Forum to Full Width