简体   繁体   English

在C#中更改时区

[英]Change timezone in C#

I have a date format, something similar to: 我有一个日期格式,类似于:

Mon, Sun, 22 Aug 2010 12:38:33 GMT 周一,周日,2010年8月22日12:38:33 GMT

How do I convert this to EST format? 如何将其转换为EST格式?

Note: I know it can be achieved using TimeZoneinfo, but that is introduced in 3.5, i want to do it 2.0 or any old version. 注意:我知道它可以使用TimeZoneinfo实现,但是在3.5中引入,我想要做2.0或任何旧版本。

I guess you have to go the old road of understanding timezones. 我想你必须走老路了解时区。 Your example is though pretty easy. 你的例子很简单。

GMT is UTC with daylight saving changes. GMT是UTC,具有夏令时变化。
EST is UTC -5hrs with daylight saving changes. EST为-5小时,夏令时更改。

So you just have to substract 5 hours to get the time in EST. 所以你只需要减去5个小时来获得EST的时间。

The simplest way would probably be to just take 5 hours off the time like so. 最简单的方法可能是花费5个小时的时间。

DateTime dateTime = DateTime.UtcNow;
dateTime.AddHours(-5);

But this won't account for daylight savings etc. But you could always code for that possibility. 但这不能解释夏令时等问题。但是你可以随时编写这种可能性的代码。

For Net 2.0 and 1.1 you could use the TimeZone class which has now been replaced by the TimeZoneInfo class. 对于Net 2.0和1.1,您可以使用TimeZone类,该类现在已被TimeZoneInfo类替换。

Edit after further research It would appear that prior to NET 3.5 all that was available was the TimeZone class. 进一步研究后编辑看来,在.NET 3.5之前,所有可用的都是TimeZone类。 It is not possible to calculate the TimeDate value for timezones other than the local one and UTC. 无法计算本地和UTC之外的时区的TimeDate值。 So if you wish to calculate the time as EST or PST and you're based in the UK then things become a little more difficult. 因此,如果您希望将时间计算为EST或PST并且您位于英国,那么事情会变得更加困难。

Here's a short demo program for using TimeZone (its from a basic console app.: 这是一个使用TimeZone的简短演示程序(它来自一个基本的控制台应用程序:

class Program
{
    static void Main(string[] args)
    {
        DateTime time = DateTime.UtcNow;
        Console.WriteLine(string.Format("UTC time is {0}", time.ToShortTimeString()));

        TimeZone zone = TimeZone.CurrentTimeZone;

        //The following line depends on a call to TimeZone.GetUtcOffset for NET 1.0 and 1.1
        DateTime workingTime = zone.ToLocalTime(time);
        DisplayTime(workingTime, zone);
        IFormatProvider culture = new System.Globalization.CultureInfo("en-GB", true);
        workingTime = DateTime.Parse("22/2/2010 12:15:32");

        Console.WriteLine();
        Console.WriteLine(string.Format("Historical Date Time is : {0}",workingTime.ToString()));
        DisplayTime(workingTime, zone);
        Console.WriteLine("Press any key to close ...");
        Console.ReadLine();
    }
    static void DisplayTime(DateTime time, TimeZone zone)
    {
        Console.WriteLine(string.Format("Current time zone is {0}", zone.StandardName));
        Console.WriteLine(string.Format("Does this time include Daylight saving? - {0}", zone.IsDaylightSavingTime(time) ? "Yes" : "No"));
        if (zone.IsDaylightSavingTime(time))
        {
            Console.WriteLine(string.Format("So this time locally is {0} {1}", time.ToShortTimeString(), zone.DaylightName));
        }
        else
        {
            Console.WriteLine(string.Format("So this time locally is {0} {1}", time.ToShortTimeString(), zone.StandardName));
        }
        Console.WriteLine(string.Format("Time offset from UTC is {0} hours.", zone.GetUtcOffset(time)));

    }
}

As you can see it only deals with local time and UTC. 如您所见,它只处理当地时间和UTC。

EST is probably not a format but an abbreviation of the time zone. EST可能不是格式,而是时区的缩写。 Ie the "manual" way would be to parse the above date and subtract the time zone difference. 即“手动”方式是解析上述日期并减去时区差异。 You should be beware of daylight saving time periods, ie you need to check if the above time fits the DST period or not. 您应该注意夏令时,即您需要检查上述时间是否符合夏令时期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM