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

方法會根據其time Kind屬性來解讀的時區。The method interprets the time zone of time based on its Kind property. 如果Kind屬性的值為或DateTimeKind.Unspecified, DateTimeKind.Local則方法會傳回當地時區的位移。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.

方法會對應TimeZoneInfo.GetUtcOffset至方法。 GetUtcOffsetThe 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.

適用於

另請參閱