PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Need Help Converting time between two timezones.-VBForums
Results 1 to 6 of 6

Thread: Need Help Converting time between two timezones.

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2017

    Need Help Converting time between two timezones.

    Hi everyone,
    First off i really apprecaite the time you are taking to help me.

    im writing a simple (or so i thought) app for a work collegue
    i want to convert a user defined time to Eastern Standard time. e.g

    user inputs the hours and minutes of a time in 24 hour format.
    this variable is then declared as either IST, PST or GMT

    this value is then converted to EST and displayed by a label.

    it will be used for reporting.
    Im using a drop down box for differentiating between IST PST and GMT
    I've been trying to figure my way around the convertime command but cant seem to work out what arguments it wants.
    can you help.
    i'll post what code I'm using (please be nice, I'm not claiming to be an expert and am willing to learn)
    Dim timeinput as date
            Dim hour As Integer
            Dim minute As Integer
            Dim second As Integer
            hour = TextBox1.Text
            minute = TextBox2.Text
            second = 0
            timeinput = $"{hour}:{minute}:{second}"
            Label1.Text = timeinput.ToString("HH:mm:ss")
            Dim istZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")
            Dim istTime As Date
            istTime = timeinput
            Label3.Text = istTime.ToString("HH:mm:ss")
            Dim estZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
            Dim estTime As DateTimeOffset = TimeZoneInfo.ConvertTime(istTime, estZone)
                           estZone.DaylightName, estZone.StandardName)
            Label2.Text = estTime.ToString("HH:mm:ss")
    if there is an easier way please help me

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002

    Re: Need Help Converting time between two timezones.

    Thread moved from CodeBank, which is for posting finished code snippets rather than asking questions.

    There may be an easier way to do this. If the difference between any of the chosen time zones and the EST zone is just a set number of hours, you could just use the AddHours method of the TimeSpan. This probably won't work, though, because the EST would be involved with the US daylight savings time thingy, so the difference in hours probably changes by an hour at different times of the year. I wouldn't expect IST to deal with that, and GMT won't, but what is PST? If that is Pacific Standard Time, then it could be MUCH more difficult, because that would be a US zone, and the observance of daylight savings time is a moving target within the US (though almost everybody observes it, by now).
    My usual boring signature: Nothing

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: Need Help Converting time between two timezones.

    Here's a simple example of converting from IST to EST: Code:
    1. Dim sourceTimeZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")
    2. Dim destinationTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
    3. Dim sourceDateTime As New DateTime(Date.Now.Ticks, DateTimeKind.Unspecified)
    4. Dim destinationDateTime = TimeZoneInfo.ConvertTime(sourceDateTime, sourceTimeZone, destinationTimeZone)
    The second and third arguments to ConvertTime are the time zones you're converting from and to. The first argument is a DateTime that represents the date and time in the time zone that you're converting from. If you're specifying a source time zone rather than converting from the local system time zone, you need the Kind of the DateTime you provide to be Unspecified.

    In your case, if all you care about is the time, then you should use the current date. That ensures that current daylight saving rules will be used. Code:
    1. Dim currentDate = Date.Today
    2. Dim sourceDateTime As New DateTime(currentDate.Year,
    3.                                    currentDate.Month,
    4.                                    currentDate.Day,
    5.                                    hour,
    6.                                    minute,
    7.                                    second,
    8.                                    DateTimeKind.Unspecified)
    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

  4. #4
    You don't want to know.
    Join Date
    Aug 2010

    Re: Need Help Converting time between two timezones.

    Note there are some quirks to what JMC is saying, and in addition you should NEVER "just add an offset" yourself like SH said unless you want to learn some real horrors of how Daylight Savings works.

    For example, a big pet peeve of mine is when someone says their timezone is "PST". Pacific Standard Time is only observed in some parts of the country for some parts of the year. Half they time they're actually observing "PDT", or "Pacific Daylight Time". The true name of their time zone is "Pacific", and whether they are observing DST changes both by region and by time. This is painfully true in places like Arizona, where the time zone is not uniform across the state. Some parts of Arizona observe MST and MDT based on the time of year. Other parts of Arizona observe MST for part of the year and PST for part of the year, because they do not observe Daylight Savings. Don't even get me started on the regions of the world where the offset between time zones is less than an hour. Or regions that observe DST at different times.

    As for JMC's code: it's as close to right as you will get, I've always been nervous about the behavior of DateTimeKind.Unspecified but I bet if I went and peeked at Reference Source I'd find what he's saying is true. The last time I dealt with this bit of code I was trying to do something a little different, anyway. I'm nervous about the second snippet, but not nervous enough to disagree. I'm going source diving when I get home.

    The thing that is quirky is:

    The world defined an international standard list of names for each time zone, and every environment on the planet adopted that list. Except Windows. Microsoft created their own name for almost every time zone in that list, so you have to know the Microsoft name. That is a royal pain in the butt if you're trying to write an application for the Mono Framework, because when you are on a Mac OS or Linux system you will be using the international standard names, not the Microsoft names, and it is a heck of a journey to go from, say, "a latitude/longitude pair" to "a time zone offset at a specific date and time." if you're trying to write a tool for a team that uses both Windows and MacOS.

    So don't expect that implementation to be portable. It's not a big deal if you're only ever going to run the app on Windows, but it's a pain if you plan to support other platforms.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Chelmsford UK

    Re: Need Help Converting time between two timezones.

    Here's a definitive version, which converts times and dates by timezone, and also applies any current daylight savings offsets...

    Name:  04-11-2015_05.49.01.jpg
Views: 64
Size:  31.5 KB

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2017

    Re: Need Help Converting time between two timezones.

    This is exactly what I needed. I couldn't understand the arguments passed to the convertime function on the MSDN page. It made no sense, thank you!

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