TimeZone.GetUtcOffset(DateTime) 方法

定义

返回指定本地时间的协调世界时 (UTC) 偏移量。Returns the Coordinated Universal Time (UTC) offset for the specified local time.

public:
 abstract TimeSpan GetUtcOffset(DateTime time);
public abstract TimeSpan GetUtcOffset (DateTime time);
abstract member GetUtcOffset : DateTime -> TimeSpan
Public MustOverride Function GetUtcOffset (time As DateTime) As TimeSpan

参数

time
DateTime

日期和时间值。A date and time value.

返回

time 相比的协调世界时 (UTC) 偏移量。The Coordinated Universal Time (UTC) offset from time.

注解

此方法返回 time 参数与协调世界时(UTC)之间的偏移量。This method returns the offset, or difference, between the time parameter and Coordinated Universal Time (UTC). 即:That is:

time = UTC + 偏移量time = UTC + offset

方法基于其 Kind 属性解释 time 的时区。The method interprets the time zone of time based on its Kind property. 如果 Kind 属性的值 DateTimeKind.LocalDateTimeKind.Unspecified,则该方法返回本地时区的偏移量。If the value of the Kind property is DateTimeKind.Local or DateTimeKind.Unspecified, the method returns the offset of the local time zone. 如果 DateTimeKind.UtcKind 属性的值,则该方法将返回等于 TimeSpan.Zero的偏移量。If the value of the Kind property is DateTimeKind.Utc, the method returns an offset equal to TimeSpan.Zero.

如果本地时区遵循夏令时,则 GetUtcOffset 在确定本地时区的偏移量时,将当前调整规则应用到 timeIf the local time zone observes daylight saving time, GetUtcOffset applies the current adjustment rule to time when determining the offset of the local time zone. 也就是说,GetUtcOffset 返回的偏移量反映 time 是否处于时区的标准时间或其夏令时。That is, the offset returned by GetUtcOffset reflects whether time falls in the time zone's standard time or its daylight saving time.

备注

GetUtcOffset 方法仅识别本地时区的当前夏令时调整规则。The GetUtcOffset method recognizes only the current daylight saving time adjustment rule for the local time zone. 因此,只有在最新调整规则生效的时间段内,才能保证准确返回本地时间的 UTC 偏移量。As a result, it is guaranteed to accurately return the UTC offset of a local time only during the period in which the latest adjustment rule is in effect. 如果 time 是受前一调整规则制约的历史日期和时间值,则可能会返回不准确的结果。It may return inaccurate results if time is a historic date and time value that was subject to a previous adjustment rule.

GetUtcOffset 方法对应于 TimeZoneInfo.GetUtcOffset 方法。The GetUtcOffset method corresponds to the TimeZoneInfo.GetUtcOffset method. 请尽可能使用 TimeZoneInfo.GetUtcOffset 方法。Whenever possible, use the TimeZoneInfo.GetUtcOffset method.

由于 time 所表示的日期和时间值与此值相对于 UTC 的偏移量未紧密耦合,因此在不同的计算机上运行或在同一台计算机上的不同时区下运行时,本地或未指定的日期和时间值可以返回不同的偏移量值。Because the date and time value represented by time and this value's offset from UTC are not tightly coupled, a local or unspecified date and time value can return a different offset value when run on different computers or when run on the same computer under different time zones. 如果不需要此行为,请改用 DateTimeOffset 值。If this behavior is undesirable, use a DateTimeOffset value instead. DateTimeOffset 数据类型会将日期和时间值与它相对于 UTC 的偏移量紧密耦合在一起。The DateTimeOffset data type tightly couples a date and time value with its offset from UTC.

适用于

另请参阅