TimeZone.GetUtcOffset(DateTime) 方法

定義

傳回指定之當地時間的 Coordinated Universal Time (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.

傳回

Coordinated Universal Time (UTC) 之於 time 的位移。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. 如果 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 會在判斷當地時區的時差時,將目前的調整規則套用至 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.

適用於

另請參閱