TimeZone.GetUtcOffset(DateTime) TimeZone.GetUtcOffset(DateTime) TimeZone.GetUtcOffset(DateTime) TimeZone.GetUtcOffset(DateTime) Method

定义

返回指定本地时间的协调世界时 (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 DateTime DateTime 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

方法time基于其Kind属性解释时区。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. 如果Kind属性的值为DateTimeKind.Utc, 则此方法TimeSpan.Zero返回等于的偏移量。If the value of the Kind property is DateTimeKind.Utc, the method returns an offset equal to TimeSpan.Zero.

如果本地时区遵循夏令时, 则当确定本地GetUtcOffset时区的偏移量时, time将当前调整规则应用到。If 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.

适用于

另请参阅