DateTimeOffset.ToLocalTime 方法

定义

将当前的 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 对象,该对象将日期和时间设置为 MaxValueMinValue ,并将偏移量设置为本地时区偏移量。

适用于

另请参阅