如何在C#中将日期时间转换为特定时区? [英] How to convert a datetime to specific timezone in c#?

查看:399
本文介绍了如何在C#中将日期时间转换为特定时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 DateTime 转换为特定时区的帮助。我下面的内容无法正常工作。

I need help converting a DateTime to a specific time zone. What I have below is not working correctly.

gmTime = 2013年3月2日1:00:00 AM

 TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
 var time = timeZoneInfo.ConvertTime(gmTime, timeZone);

当我调试 time 的值时,应用区域时,应为 03/01/2013 PM:$ ,返回为 03/02/2013 1: 00:00 AM

When I debug the value of time, which should be 03/01/2013 8:00:00 PM when the zone is applied, it comes back as 03/02/2013 1:00:00 AM.

如果我执行 time.ToLocalTime(),那么我会得到正确的值。但是,我需要将时间转换为不同的时区。

If I do time.ToLocalTime() then I get the correct value. However, I need to convert time to different time zones.

推荐答案

DateTime对象具有一个种类变量,可帮助TimeZoneInfo知道如何处理它。在TimeZone.ConvertTime的 MSDN文档中,它具有以下内容:

DateTime objects have a "Kind" variable which helps TimeZoneInfo know how to treat it. In the MSDN documentation for TimeZone.ConvertTime it has the following:


DateTimeKind.Local,将本地时间转换为destinationTimeZone中的时间。

DateTimeKind.Local, Converts the local time to the time in destinationTimeZone.

DateTimeKind.Utc,转换坐标

DateTimeKind.Utc, Converts Coordinated Universal Time (UTC) to the time in destinationTimeZone.

DateTimeKind。未指定,假定是本地时间。

DateTimeKind.Unspecified, Assumed to be Local.

例如:

  Console.WriteLine("Local time zone is '{0}'.", TimeZoneInfo.Local.Id);

  var gmTime          = new DateTime(2013, 03, 02, 01, 00, 00, DateTimeKind.Utc);
  var localTime       = new DateTime(2013, 03, 02, 01, 00, 00, DateTimeKind.Local);
  var unspecifiedTime = new DateTime(2013, 03, 02, 01, 00, 00);

  var timeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

  var gmTimeConverted           = TimeZoneInfo.ConvertTime(gmTime,          timeZone); // 03/02/2013 8:00:00AM
  var localTimeConverted        = TimeZoneInfo.ConvertTime(localTime,       timeZone); // 03/02/2013 
  var unspecifiedTimeConverted  = TimeZoneInfo.ConvertTime(unspecifiedTime, timeZone);

  Console.WriteLine("Converting GMT         to EST: {0}", gmTimeConverted);
  Console.WriteLine("Converting Local       to EST: {0}", localTimeConverted);
  Console.WriteLine("Converting Unspecified to EST: {0}", unspecifiedTimeConverted);

结果:


Local time zone is 'Pacific Standard Time'.
Converting GMT         to EST: 3/1/2013 8:00:00 PM
Converting Local       to EST: 3/2/2013 4:00:00 AM
Converting Unspecified to EST: 3/2/2013 4:00:00 AM

或者如果您的本地时区为东部标准时间,您会得到这些结果

Or if your local timezone is 'Eastern Standard Time' you get these results


Local time zone is 'Eastern Standard Time'.
Converting GMT         to EST: 3/1/2013 8:00:00 PM
Converting Local       to EST: 3/2/2013 1:00:00 AM
Converting Unspecified to EST: 3/2/2013 1:00:00 AM





如果您想要将TimeZoneInfo像Utc一样对待未指定,则应像TimeZoneInfo.ConvertTimeFromUtc一样运行。再次从 MSDN文档


DateTimeKind.Local,引发ArgumentException。

DateTimeKind.Local, Throws an ArgumentException.

DateTimeKind.Unspecified或DateTimeKind.Utc,从协调世界时(UTC)转换。

DateTimeKind.Unspecified or DateTimeKind.Utc, Converts from Coordinated Universal Time (UTC).

这篇关于如何在C#中将日期时间转换为特定时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆