如何获得两个时区之间的当前时差

MD *_*med 11 c# asp.net

我想计算美国/中部时区和英国夏令时之间的当前时差.我的意思是,目前这两个时区都有夏令时,所以他们有6小时的时差.但是在2010年10月31日星期日之后,英国夏季的夏令时将会被取消,届时这两个时区之间将有5小时的时差.

有什么方法可以计算出这些不同的时差吗?

Jon*_*eet 22

只是为给出的答案提供一些具体的代码,这里有一些代码来解决我(在伦敦)和我在山景城的同事之间的当前差异:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Ami*_*shk 5

您可以从不同的时区创建两个日期时间对象,一个很好的例子:Creating a DateTime in a specific Time Zone in c# fx 3.5

并计算它们之间的差值。