C#日期时间:不同时区的转换

我有一堆日期时间,我跟踪我的应用程序。 它们都是UTC时间。 对于我的应用程序的一部分,我想发送一封包含这些时间之一的电子邮件,但编辑为在该特定时区。

我将只涉及两个主要领域,东海岸和德克萨斯(达拉斯和休斯顿)

当我发送这封电子邮件以获取东部时区时,我也可以创建一个新的日期时间( DateTime timestamp = DateTime.Now;

我的问题是:

如果用户在德克萨斯州,我怎样才能将我的时间从东部转换到那个时间(少1小时)?

我试过这样的事情:

  //Convert timestamp to local time TimeSpan ts = TimeZone.CurrentTimeZone.GetUtcOffset(timestamp); timestamp.Add(ts); timestampString = timestamp.ToString(); 

但那没用。 我也知道这条线无效:

 timestamp.Hour = timestamp.Hour - 1; 

使用TimeZoneInfo类将本地时间转换为备用时区中的时间:

 TimeZoneInfo est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); DateTime targetTime = TimeZoneInfo.ConvertTime(timeToConvert, est); 
  var now = DateTime.Now; // Current date/time var utcNow = now.ToUniversalTime(); // Converted utc time var otherTimezone = TimeZoneInfo.FindSystemTimeZoneById("ANY OTHER VALID TIMEZONE"); // Get other timezone var newTime = TimeZoneInfo.ConvertTimeFromUtc(utcNow, otherTimezone); // New Timezone 

这应该可以解决问题

 DateTime localTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.Local); 

使用TimeZoneInfo.ConvertTimeFromUtc 。 那里列出的例子非常自我解释。

你可以使用javascript:

  var visitortime = new Date(); vat time = visitortime.getTimezoneOffset()/60; 

之后,您可以将此值保存到runat =“server”的任何隐藏控件。

为什么不呢

TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "AUS Eastern Standard Time");

并检查获取所有可用的时区

 foreach (TimeZoneInfo tz in TimeZoneInfo.GetSystemTimeZones()) { Console.WriteLine(tz.Id); }