1
我的目标是获得UTC和另一个时区之间的区别。以UTC和EST之间的差异为例(UTC-5:00)。同样为了举例,假设我的系统目前处于太平洋标准时间,所以DateTime.Kind,“LOCAL”是PST。为了找到UTC和EST之间的差异,我不得不提供我在PST中提供的DateTime。这里是我的代码简化的片段:TimeZone.GetUtcOffset()需要DateTime,但是DateTime的时区被忽略,为什么?
public static void Run_Timezone_Test()
{
var myDate = DateTime.Now;
Console.WriteLine(myDate.Kind);
//OUTPUT: Local (note this is currently PST)
var easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var offset = easternTimeZone.GetUtcOffset(myDate);
Console.WriteLine(offset);
//OUTPUT: -05:00:00 (correct offset for EST)
Console.ReadLine();
}
为什么我不得不提供“指明MyDate”如果不使用它和它的时区?
请参阅[时区标记wiki](http://stackoverflow.com/tags/timezone/info),该说明解释(其中包括)“时区!=偏移”。 –
@MattJohnson,伟大的文章,谢谢! – danjuggler