我正在尝试使用 DateTimeOffset
来传达任何时区的特定时刻。我不知道如何使用 TimeZoneInfo
来处理夏令时。
var dt = DateTime.UtcNow;
Console.WriteLine(dt.ToLocalTime());
var tz = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var utcOffset = new DateTimeOffset(dt, TimeSpan.Zero);
Console.WriteLine(utcOffset.ToOffset(tz.BaseUtcOffset));
打印出来:
6/2/2010 4:37:19 PM 6/2/2010 3:37:19 PM -06:00
我在中部时区,我们目前处于夏令时。 我正在尝试读取第二行:
6/2/2010 4:37:19 PM -05:00
BaseUtcOffset
显然不会根据 DST 发生变化。
如何使用正确的偏移值获得正确的时间?
最佳答案
您还可以使用 TimeZoneInfo.ConvertTimeFromUtc,这将允许夏令时:
DateTime utc = DateTime.UtcNow;
TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utc, zone);
关于c# - 如何在夏令时期间使用 TimeZoneInfo 获取本地时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2961848/