Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain the current time differences between two timezones

Tags:

c#

asp.net

I want to calculate the current time differences between US/Central timezone and British Summer Time. I mean, currently these both timezones have daylight savings going on, so they have a 6 hours time difference. But after Sunday October 31 2010, daylight savings will be off for British summer time, at which moment there will be a 5 hours time differences between these two timezones.

Is there any way I can calculate these varying time differences?

like image 239
MD Sayem Ahmed Avatar asked Oct 07 '10 04:10

MD Sayem Ahmed


People also ask

How do you find the time difference between two time zones?

As a general rule of thumb, a change of 15° of longitude should result in a time difference of 1 hour. In the U.S., there are a total of 9 time zones used. Likely the most well-known include Eastern, Central, Mountain, and Pacific Time Zones.

How do we calculate the time difference?

Calculate the duration between two times The goal is to subtract the starting time from the ending time under the correct conditions. If the times are not already in 24-hour time, convert them to 24-hour time. AM hours are the same in both 12-hour and 24-hour time.


1 Answers

Just to provide some concrete code for the answers given, here's some code to work out the current difference between me (in London) and my colleagues in Mountain View:

using System;

class Test
{
    static void Main()
    {
        var london = TimeZoneInfo.FindSystemTimeZoneById
            ("GMT Standard Time");
        var googleplex = TimeZoneInfo.FindSystemTimeZoneById
            ("Pacific Standard Time");

        var now = DateTimeOffset.UtcNow;
        TimeSpan londonOffset = london.GetUtcOffset(now);
        TimeSpan googleplexOffset = googleplex.GetUtcOffset(now);
        TimeSpan difference = londonOffset - googleplexOffset;        
        Console.WriteLine(difference);
    }
}
like image 105
Jon Skeet Avatar answered Oct 12 '22 20:10

Jon Skeet