TimeZoneInfo.ConvertTimeBySystemTimeZoneId 方法

定義

根據時區識別項將時間轉換為另一時區的時間。

多載

ConvertTimeBySystemTimeZoneId(DateTime, String)

根據時區的識別項將時間轉換為另一時區的時間。

ConvertTimeBySystemTimeZoneId(DateTimeOffset, String)

根據時區的識別項將時間轉換為另一時區的時間。

ConvertTimeBySystemTimeZoneId(DateTime, String, String)

根據時區識別項將時間從一個時區轉換為另一個時區。

ConvertTimeBySystemTimeZoneId(DateTime, String)

Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs

根據時區的識別項將時間轉換為另一時區的時間。

public:
 static DateTime ConvertTimeBySystemTimeZoneId(DateTime dateTime, System::String ^ destinationTimeZoneId);
public static DateTime ConvertTimeBySystemTimeZoneId (DateTime dateTime, string destinationTimeZoneId);
static member ConvertTimeBySystemTimeZoneId : DateTime * string -> DateTime
Public Shared Function ConvertTimeBySystemTimeZoneId (dateTime As DateTime, destinationTimeZoneId As String) As DateTime

參數

dateTime
DateTime

要轉換的日期和時間。

destinationTimeZoneId
String

目的地時區的識別項。

傳回

目的地時區中的日期與時間。

例外狀況

destinationTimeZoneIdnull

找到時區識別項,但登錄資料已損毀。

處理序沒有必要權限可讀取包含時區資訊的登錄機碼。

在本機系統上找不到 destinationTimeZoneId 識別項。

備註

執行轉換時, ConvertTimeBySystemTimeZoneId 此方法會套用時區中 destinationTimeZoneId 生效的任何調整規則。

這個多載與呼叫 ConvertTime(DateTime, TimeZoneInfo) 方法大致相同,不同之處在于它可讓您依其識別碼指定目的地時區,而不是物件參考。 當您必須轉換時間而不擷取對應的時區物件,而且您不需要知道轉換的時間是標準時間還是日光節約時間時,這個方法最有用。

方法 ConvertTimeBySystemTimeZoneId(DateTime, String) 會從 dateTime 參數 Kind 的 屬性值判斷來源時區,如下表所示。

Kind 屬性值 來源時區 方法行為
DateTimeKind.Local Local 將當地時間轉換為 中的 destinationTimeZone 時間。
DateTimeKind.Utc Utc 將國際標準時間 (UTC) 轉換為 中的 destinationTimeZone 時間。
DateTimeKind.Unspecified 假設為 Local 將當地時間轉換為 中的 destinationTimeZone 時間。

KindDateTime 值的 屬性會設定如下表所示。

條件 傳回的 Kind 屬性值
destinationTimeZoneTimeZoneInfo.Utc.Id DateTimeKind.Utc
任何其他 destinationTimeZone 值。 DateTimeKind.Unspecified

如果參數的值是模棱兩可的 dateTime 當地時間,則會解譯為標準時間。 dateTime如果參數是不正確當地時間,這個方法會擲回 ArgumentException

如果轉換 dateTime 結果的日期和時間值早于 DateTime.MinValue 或 晚 DateTime.MaxValue ,則此方法會分別傳回 DateTime.MinValueDateTime.MaxValue

這個方法會擷取時區的相關資訊,其識別碼是由 destinationTimeZoneId Windows 系統上登錄的參數所指定,以及從 Linux 和 macOS 上的 ICU 程式庫 指定。 它無法擷取使用 方法建立的 CreateCustomTimeZone 時區物件。 參數 destinationTimeZoneId 必須完全對應到時區的識別碼長度,但不會在發生成功比對的情況下對應;也就是說,與時區識別碼的比較 destinationTimeZoneId 不區分大小寫。

另請參閱

適用於

ConvertTimeBySystemTimeZoneId(DateTimeOffset, String)

Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs

根據時區的識別項將時間轉換為另一時區的時間。

public:
 static DateTimeOffset ConvertTimeBySystemTimeZoneId(DateTimeOffset dateTimeOffset, System::String ^ destinationTimeZoneId);
public static DateTimeOffset ConvertTimeBySystemTimeZoneId (DateTimeOffset dateTimeOffset, string destinationTimeZoneId);
static member ConvertTimeBySystemTimeZoneId : DateTimeOffset * string -> DateTimeOffset
Public Shared Function ConvertTimeBySystemTimeZoneId (dateTimeOffset As DateTimeOffset, destinationTimeZoneId As String) As DateTimeOffset

參數

dateTimeOffset
DateTimeOffset

要轉換的日期和時間。

destinationTimeZoneId
String

目的地時區的識別項。

傳回

目的地時區中的日期與時間。

例外狀況

destinationTimeZoneIdnull

找到時區識別項,但登錄資料已損毀。

處理序沒有必要權限可讀取包含時區資訊的登錄機碼。

在本機系統上找不到 destinationTimeZoneId 識別項。

備註

執行轉換時, ConvertTimeBySystemTimeZoneId 此方法會套用時區中 destinationTimeZoneId 生效的任何調整規則。

這個多載與呼叫 ConvertTime(DateTimeOffset, TimeZoneInfo) 方法相同,不同之處在于它可讓您依其識別碼指定目的地時區,而不是由物件參考指定。 當您必須轉換時間而不擷取對應的時區物件,而且您不需要知道轉換的時間是標準時間還是日光節約時間時,這個方法最有用。

dateTimeOffset因為 參數代表日期和時間,以及該時間與國際標準時間的位移 (UTC) ,所以它不能代表模棱兩可的時間或不正確時間。

這個方法會從 Windows 系統上的登錄和 Linux 和 macOS 上的ICU 程式庫擷取其識別碼所 destinationTimeZoneId 指定的時區。 它無法擷取使用 方法建立的 CreateCustomTimeZone 時區物件。 參數 destinationTimeZoneId 必須完全對應到時區的識別碼長度,但不會在發生成功比對的情況下對應;也就是說,與時區識別碼的比較 destinationTimeZoneId 不區分大小寫。

在將值轉換成 dateTimeOffset 目的地時區的時間時,此方法會考慮目的地時區中生效的任何調整規則。

如果轉換 dateTimeOffset 結果的日期和時間值早于 DateTimeOffset.MinValue 或 晚 DateTimeOffset.MaxValue ,則此方法會分別傳回 DateTimeOffset.MinValueDateTimeOffset.MaxValue

另請參閱

適用於

ConvertTimeBySystemTimeZoneId(DateTime, String, String)

Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs
Source:
TimeZoneInfo.cs

根據時區識別項將時間從一個時區轉換為另一個時區。

public:
 static DateTime ConvertTimeBySystemTimeZoneId(DateTime dateTime, System::String ^ sourceTimeZoneId, System::String ^ destinationTimeZoneId);
public static DateTime ConvertTimeBySystemTimeZoneId (DateTime dateTime, string sourceTimeZoneId, string destinationTimeZoneId);
static member ConvertTimeBySystemTimeZoneId : DateTime * string * string -> DateTime
Public Shared Function ConvertTimeBySystemTimeZoneId (dateTime As DateTime, sourceTimeZoneId As String, destinationTimeZoneId As String) As DateTime

參數

dateTime
DateTime

要轉換的日期和時間。

sourceTimeZoneId
String

來源時區的識別項。

destinationTimeZoneId
String

目的地時區的識別項。

傳回

目的地時區中的日期和時間,對應至來源時區中的 dateTime 參數。

例外狀況

dateTime 參數的 Kind 屬性未對應至來源時區。

-或-

dateTime 是來源時區中的無效時間。

sourceTimeZoneIdnull

-或-

destinationTimeZoneIdnull

找到時區識別項,但登錄資料已損毀。

使用者沒有必要權限可讀取保留時區資料的登錄機碼。

在本機系統上找不到 sourceTimeZoneId 識別項。

-或-

在本機系統上找不到 destinationTimeZoneId 識別項。

範例

下列範例會 TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime, String, String) 使用 方法來顯示對應至全球八個城市當地系統時間的時間。

DateTime currentTime = DateTime.Now;
Console.WriteLine("Current Times:");
Console.WriteLine();
Console.WriteLine("Los Angeles: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Pacific Standard Time"));
Console.WriteLine("Chicago: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Central Standard Time"));
Console.WriteLine("New York: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Eastern Standard Time"));
Console.WriteLine("London: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "GMT Standard Time"));
Console.WriteLine("Moscow: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Russian Standard Time"));
Console.WriteLine("New Delhi: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "India Standard Time"));
Console.WriteLine("Beijing: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "China Standard Time"));
Console.WriteLine("Tokyo: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Tokyo Standard Time"));
let currentTime = DateTime.Now
printfn "Current Times:\n"
printfn $"""Los Angeles: {TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Pacific Standard Time")}"""
printfn $"""Chicago: {TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Central Standard Time")}"""
printfn $"""New York: {TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Eastern Standard Time")}"""
printfn $"""London: {TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "GMT Standard Time")}"""
printfn $"""Moscow: {TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Russian Standard Time")}"""
printfn $"""New Delhi: {TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "India Standard Time")}"""
printfn $"""Beijing: {TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "China Standard Time")}"""
printfn $"""Tokyo: {TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Tokyo Standard Time")}"""
Dim currentTime As Date = Date.Now
Console.WriteLine("Current Times:")
Console.WriteLine()
Console.WriteLine("Los Angeles: {0}", _
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Pacific Standard Time"))
Console.WriteLine("Chicago: {0}", _ 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Central Standard Time"))
Console.WriteLine("New York: {0}", _
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Eastern Standard Time"))
Console.WriteLine("London: {0}", _
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "GMT Standard Time"))
Console.WriteLine("Moscow: {0}", _
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Russian Standard Time"))
Console.WriteLine("New Delhi: {0}", _
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "India Standard Time"))
Console.WriteLine("Beijing: {0}", _
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "China Standard Time"))
Console.WriteLine("Tokyo: {0}", _
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Tokyo Standard Time"))

備註

執行轉換時, ConvertTimeBySystemTimeZoneId 此方法會套用時區中 destinationTimeZoneId 生效的任何調整規則。

雖然它與 TimeZoneInfo.ConvertTime(DateTime, TimeZoneInfo, TimeZoneInfo) 方法類似,但您可以使用 TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime, String, String) 其識別碼來指定來源和目的地時區,而不是其 TimeZoneInfo 物件。 當您必須轉換時間而不擷取對應的時區物件,而且您不需要知道轉換的時間是標準時間還是日光節約時間時,這個方法最有用。

這個方法會擷取其識別碼是 sourceTimeZoneId Windows 系統上登錄的 和 destinationTimeZoneId 參數,以及從 Linux 和 macOS 上的 ICU 程式庫 擷取時區。 它無法擷取使用 方法建立的 CreateCustomTimeZone 時區物件。

參數的 KinddateTime 屬性值必須對應至 sourceTimeZoneId 參數,如下表所示。

DateTime.Kind 值 sourceTimeZone 值 方法行為
DateTimeKind.Utc TimeZoneInfo.Utc.Id等於 。 dateTime轉換為目的地時區的時間。
DateTimeKind.Utc Does not equal TimeZoneInfo.Utc.Id 擲回 ArgumentException
DateTimeKind.Local TimeZoneInfo.Local.Id等於 。 dateTime轉換為目的地時區的時間。
DateTimeKind.Local Does not equal TimeZoneInfo.Local.Id 擲回 ArgumentException
DateTimeKind.Unspecified 任何。 dateTime轉換為目的地時區的時間。

因為它依賴對 方法的 FindSystemTimeZoneById 呼叫,所以 ConvertTimeBySystemTimeZoneId 方法會執行不區分大小寫的搜尋,以找出對應至 sourceTimeZoneIddestinationTimeZoneId 的時區。

如果參數的值 dateTime 在來源時區中是模棱兩可的時間,則會解譯為標準時間。 dateTime如果參數在來源時區中是不正確時間,這個方法會擲回 ArgumentException

Kind除非目的地時區是國際標準時間 (UTC) ,否則傳 DateTime 回值的 屬性會設定 DateTimeKind.Unspecified 為 ,在此情況下,它會設定為 DateTimeKind.Utc

另請參閱

適用於