DateTimeOffset.ToLocalTime 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將目前 DateTimeOffset 物件轉換為表示本地時間的 DateTimeOffset 物件。
public:
DateTimeOffset ToLocalTime();
public DateTimeOffset ToLocalTime ();
member this.ToLocalTime : unit -> DateTimeOffset
Public Function ToLocalTime () As DateTimeOffset
傳回
物件,表示轉換為本地時間之目前 DateTimeOffset 物件的日期和時間。
範例
下列範例會 ToLocalTime 使用 方法,將值轉換成 DateTimeOffset 太平洋標準時區中的當地時間。 它也說明方法對當地時區調整規則的支援。
// Local time changes on 3/11/2007 at 2:00 AM
DateTimeOffset originalTime, localTime;
originalTime = new DateTimeOffset(2007, 3, 11, 3, 0, 0,
new TimeSpan(-6, 0, 0));
localTime = originalTime.ToLocalTime();
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(),
localTime.ToString());
originalTime = new DateTimeOffset(2007, 3, 11, 4, 0, 0,
new TimeSpan(-6, 0, 0));
localTime = originalTime.ToLocalTime();
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(),
localTime.ToString());
// Define a summer UTC time
originalTime = new DateTimeOffset(2007, 6, 15, 8, 0, 0,
TimeSpan.Zero);
localTime = originalTime.ToLocalTime();
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(),
localTime.ToString());
// Define a winter time
originalTime = new DateTimeOffset(2007, 11, 30, 14, 0, 0,
new TimeSpan(3, 0, 0));
localTime = originalTime.ToLocalTime();
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(),
localTime.ToString());
// The example produces the following output:
// Converted 3/11/2007 3:00:00 AM -06:00 to 3/11/2007 1:00:00 AM -08:00.
// Converted 3/11/2007 4:00:00 AM -06:00 to 3/11/2007 3:00:00 AM -07:00.
// Converted 6/15/2007 8:00:00 AM +00:00 to 6/15/2007 1:00:00 AM -07:00.
// Converted 11/30/2007 2:00:00 PM +03:00 to 11/30/2007 3:00:00 AM -08:00.
// Local time changes on 3/11/2007 at 2:00 AM
let originalTime = DateTimeOffset(2007, 3, 11, 3, 0, 0, TimeSpan(-6, 0, 0))
let localTime = originalTime.ToLocalTime()
printfn $"Converted {originalTime} to {localTime}."
let originalTime = DateTimeOffset(2007, 3, 11, 4, 0, 0, TimeSpan(-6, 0, 0))
let localTime = originalTime.ToLocalTime()
printfn $"Converted {originalTime} to {localTime}."
// Define a summer UTC time
let originalTime = DateTimeOffset(2007, 6, 15, 8, 0, 0, TimeSpan.Zero)
let localTime = originalTime.ToLocalTime()
printfn $"Converted {originalTime} to {localTime}."
// Define a winter time
let originalTime = DateTimeOffset(2007, 11, 30, 14, 0, 0, TimeSpan(3, 0, 0))
let localTime = originalTime.ToLocalTime()
printfn $"Converted {originalTime} to {localTime}."
// The example produces the following output:
// Converted 3/11/2007 3:00:00 AM -06:00 to 3/11/2007 1:00:00 AM -08:00.
// Converted 3/11/2007 4:00:00 AM -06:00 to 3/11/2007 3:00:00 AM -07:00.
// Converted 6/15/2007 8:00:00 AM +00:00 to 6/15/2007 1:00:00 AM -07:00.
// Converted 11/30/2007 2:00:00 PM +03:00 to 11/30/2007 3:00:00 AM -08:00.
' Local time changes on 3/11/2007 at 2:00 AM
Dim originalTime, localTime As DateTimeOffset
originalTime = New DateTimeOffset(#03/11/2007 3:00AM#, _
New TimeSpan(-6, 0, 0))
localTime = originalTime.ToLocalTime()
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(), _
localTime.ToString())
originalTime = New DateTimeOffset(#03/11/2007 4:00AM#, _
New TimeSpan(-6, 0, 0))
localTime = originalTime.ToLocalTime()
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(), _
localTime.ToString())
' Define a summer UTC time
originalTime = New DateTimeOffset(#6/15/2007 8:00AM#, _
TimeSpan.Zero)
localTime = originalTime.ToLocalTime()
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(), _
localTime.ToString())
' Define a winter time
originalTime = New DateTimeOffset(#11/30/2007 2:00PM#, _
New TimeSpan(3, 0, 0))
localTime = originalTime.ToLocalTime()
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(), _
localTime.ToString())
' The example produces the following output:
' Converted 3/11/2007 3:00:00 AM -06:00 to 3/11/2007 1:00:00 AM -08:00.
' Converted 3/11/2007 4:00:00 AM -06:00 to 3/11/2007 3:00:00 AM -07:00.
' Converted 6/15/2007 8:00:00 AM +00:00 to 6/15/2007 1:00:00 AM -07:00.
' Converted 11/30/2007 2:00:00 PM +03:00 to 11/30/2007 3:00:00 AM -08:00.
備註
在執行本機時間轉換時,方法會先將目前 DateTimeOffset 物件的日期和時間轉換為國際標準時間 (UTC) ,方法是從時間減去位移。 然後,它會藉由新增當地時區位移,將 UTC 日期和時間轉換為當地時間。 如此一來,它會考慮當地時區的任何調整規則。
目前 DateTimeOffset 物件的值和方法呼叫所傳回之物件的值 DateTimeOffset 都代表相同的時間點。 也就是說,如果兩者都傳遞至 DateTimeOffset.Equals(DateTimeOffset, DateTimeOffset) 方法,方法會傳回 true
。
如果轉換造成超出型別範圍 DateTimeOffset 的時間,此方法 ToLocalTime 會傳回 DateTimeOffset 物件,該物件會將日期和時間設定為 MaxValue 或 MinValue ,並將位移設定為當地時區位移。