16

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?

MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
  • Possible duplicate of [Creating a DateTime in a specific Time Zone in c# fx 3.5](https://stackoverflow.com/questions/246498/creating-a-datetime-in-a-specific-time-zone-in-c-sharp-fx-3-5) – Liam Feb 08 '18 at 08:57

4 Answers4

27

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);
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • except Mountain View isn't in US Mountain Standard Time :-) – Andrew Oct 07 '10 at 06:32
  • @Andrew: Is it not? This is the problem with Windows IDs instead of zoneinfo names :( What's the right ID in this case? Just "Mountain Standard Time"? Or "Pacific Standard Time"? Editing to the latter... – Jon Skeet Oct 07 '10 at 06:39
  • sorry, I should have been more explicit in my comment. I believe it should be Pacific Standard Time. – Andrew Oct 07 '10 at 06:47
  • @Andrew: No problem. Glad my third guess was correct... not sure why I didn't plump for Pacific to start with. – Jon Skeet Oct 07 '10 at 06:53
5

You can create two datetime object from different timezones, a good example: Creating a DateTime in a specific Time Zone in c# fx 3.5

And calculate the Delta between them.

Community
  • 1
  • 1
Amirshk
  • 8,170
  • 2
  • 35
  • 64
  • Though I haven't used that exact method, but from your answers I got the basic idea. I have implemented that solution and it is almost 90% similar to Jon Skeet's answer. But since your answer gave me the idea, I am selecting it as the chosen answer. Thank you very much :-) – MD Sayem Ahmed Oct 08 '10 at 06:27
2

Am has correct answer which will mostly work if you limit yourself to current date. In general depending on how correct you want to be it can be quite hard. This is because the DST in a timezone as of today can be different than what it was in history. Maybe before 01/04/1950 there was no British Summer Time. Or during the Sydney Olympics the DST dates were temporarily changed in year 2000. To take all these things into account you will need a historical DST/TimeZone database.

softveda
  • 10,858
  • 6
  • 42
  • 50
  • No I don't need historical DST/TimeZone database, I have computed the time differences in these two areas and then used it for the necessary conversion, but thanks for your answer. – MD Sayem Ahmed Oct 08 '10 at 06:26
2

The documentation for TimeZoneInfo.GetUtcOffset has an example that shows the calculation of the UTC offset for various time zones at different times. You just need to get the UTC Offset at the specific time of interest for each of your two time zones and compute the difference. See:

http://msdn.microsoft.com/en-us/library/bb396378.aspx

Andrew
  • 721
  • 4
  • 14