DateTimeOffset.LocalDateTime 屬性

定義

取得 DateTime 值,這個值表示目前 DateTimeOffset 物件的本地日期和時間。

public:
 property DateTime LocalDateTime { DateTime get(); };
public DateTime LocalDateTime { get; }
member this.LocalDateTime : DateTime
Public ReadOnly Property LocalDateTime As DateTime

屬性值

DateTime

目前 DateTimeOffset 物件的當地日期和時間。

範例

下列範例說明將數個值轉換成 DateTimeOffset 美國太平洋標準時區的當地時間。 請注意,過去三次全都模棱兩可;屬性會將所有屬性對應到太平洋標準時區中的單一日期和時間。

 DateTimeOffset dto;

 // Current time
 dto = DateTimeOffset.Now;
 Console.WriteLine(dto.LocalDateTime);
 // UTC time
 dto = DateTimeOffset.UtcNow;
 Console.WriteLine(dto.LocalDateTime);

// Transition to DST in local time zone occurs on 3/11/2007 at 2:00 AM
 dto = new DateTimeOffset(2007, 3, 11, 3, 30, 0, new TimeSpan(-7, 0, 0));
 Console.WriteLine(dto.LocalDateTime);
 dto = new DateTimeOffset(2007, 3, 11, 2, 30, 0, new TimeSpan(-7, 0, 0));
 Console.WriteLine(dto.LocalDateTime);
 // Invalid time in local time zone
 dto = new DateTimeOffset(2007, 3, 11, 2, 30, 0, new TimeSpan(-8, 0, 0));
 Console.WriteLine(TimeZoneInfo.Local.IsInvalidTime(dto.DateTime));
 Console.WriteLine(dto.LocalDateTime);

 // Transition from DST in local time zone occurs on 11/4/07 at 2:00 AM
 // This is an ambiguous time
 dto = new DateTimeOffset(2007, 11, 4, 1, 30, 0, new TimeSpan(-7, 0, 0));
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto));
 Console.WriteLine(dto.LocalDateTime);
 dto = new DateTimeOffset(2007, 11, 4, 2, 30, 0, new TimeSpan(-7, 0, 0));
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto));
 Console.WriteLine(dto.LocalDateTime);
 // This is also an ambiguous time
 dto = new DateTimeOffset(2007, 11, 4, 1, 30, 0, new TimeSpan(-8, 0, 0));
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto));
 Console.WriteLine(dto.LocalDateTime);
 // If run on 3/8/2007 at 4:56 PM, the code produces the following
 // output:
 //    3/8/2007 4:56:03 PM
 //    3/8/2007 4:56:03 PM
 //    3/11/2007 3:30:00 AM
 //    3/11/2007 1:30:00 AM
 //    True
 //    3/11/2007 3:30:00 AM
 //    True
 //    11/4/2007 1:30:00 AM
 //    11/4/2007 1:30:00 AM
 //    True
 //    11/4/2007 1:30:00 AM
// Current time
let dto = DateTimeOffset.Now
printfn $"{dto.LocalDateTime}"
// UTC time
let dto = DateTimeOffset.UtcNow
printfn $"{dto.LocalDateTime}"

// Transition to DST in local time zone occurs on 3/11/2007 at 2:00 AM
let dto = DateTimeOffset(2007, 3, 11, 3, 30, 0, TimeSpan(-7, 0, 0))
printfn $"{dto.LocalDateTime}"
let dto = DateTimeOffset(2007, 3, 11, 2, 30, 0, TimeSpan(-7, 0, 0))
printfn $"{dto.LocalDateTime}"
// Invalid time in local time zone
let dto = DateTimeOffset(2007, 3, 11, 2, 30, 0, TimeSpan(-8, 0, 0))
printfn $"{TimeZoneInfo.Local.IsInvalidTime dto.DateTime}"
printfn $"{dto.LocalDateTime}"

// Transition from DST in local time zone occurs on 11/4/07 at 2:00 AM
// This is an ambiguous time
let dto = DateTimeOffset(2007, 11, 4, 1, 30, 0, TimeSpan(-7, 0, 0))
printfn $"{TimeZoneInfo.Local.IsAmbiguousTime dto}"
printfn $"{dto.LocalDateTime}"
let dto = DateTimeOffset(2007, 11, 4, 2, 30, 0, TimeSpan(-7, 0, 0))
printfn $"{TimeZoneInfo.Local.IsAmbiguousTime dto}"
printfn $"{dto.LocalDateTime}"
// This is also an ambiguous time
let dto = DateTimeOffset(2007, 11, 4, 1, 30, 0, TimeSpan(-8, 0, 0))
printfn $"{TimeZoneInfo.Local.IsAmbiguousTime dto}"
printfn $"{dto.LocalDateTime}"

// If run on 3/8/2007 at 4:56 PM, the code produces the following
// output:
//    3/8/2007 4:56:03 PM
//    3/8/2007 4:56:03 PM
//    3/11/2007 3:30:00 AM
//    3/11/2007 1:30:00 AM
//    True
//    3/11/2007 3:30:00 AM
//    True
//    11/4/2007 1:30:00 AM
//    11/4/2007 1:30:00 AM
//    True
//    11/4/2007 1:30:00 AM
 Dim dto As DateTimeOffset

 ' Current time
 dto = DateTimeOffset.Now
 Console.WriteLine(dto.LocalDateTime)
 ' UTC time
 dto = DateTimeOffset.UtcNow
 Console.WriteLine(dto.LocalDateTime)

' Transition to DST in local time zone occurs on 3/11/2007 at 2:00 AM
 dto = New DateTimeOffset(#03/11/2007 3:30AM#, New Timespan(-7, 0, 0))      
 Console.WriteLine(dto.LocalDateTime)
 dto = New DateTimeOffset(#03/11/2007 2:30AM#, New Timespan(-7, 0, 0))
 Console.WriteLine(dto.LocalDateTime)
 ' Invalid time in local time zone
 dto = New DateTimeOffset(#03/11/2007 2:30AM#, New Timespan(-8, 0, 0))
 Console.WriteLine(TimeZoneInfo.Local.IsInvalidTime(dto.DateTime))
 Console.WriteLine(dto.LocalDateTime)

 ' Transition from DST in local time zone occurs on 11/4/07 at 2:00 AM
 ' This is an ambiguous time
 dto = New DateTimeOffset(#11/4/2007 1:30AM#, New TimeSpan(-7, 0, 0))
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto))
 Console.WriteLine(dto.LocalDateTime)
 dto = New DateTimeOffset(#11/4/2007 2:30AM#, New TimeSpan(-7, 0, 0))           
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto))
 Console.WriteLine(dto.LocalDateTime)
 ' This is also an ambiguous time
 dto = New DateTimeOffset(#11/4/2007 1:30AM#, New TimeSpan(-8, 0, 0))           
 Console.WriteLine(TimeZoneInfo.Local.IsAmbiguousTime(dto))
 Console.WriteLine(dto.LocalDateTime)
 ' If run on 3/8/2007 at 4:56 PM, the code produces the following
 ' output:
 '    3/8/2007 4:56:03 PM
 '    3/8/2007 4:56:03 PM
 '    3/11/2007 3:30:00 AM
 '    3/11/2007 1:30:00 AM
 '    True
 '    3/11/2007 3:30:00 AM
 '    True
 '    11/4/2007 1:30:00 AM
 '    11/4/2007 1:30:00 AM
 '    True
 '    11/4/2007 1:30:00 AM

備註

如有必要, LocalDateTime 屬性會將目前 DateTimeOffset 物件的日期和時間轉換為本機系統的日期和時間。 轉換是雙步驟作業:

  1. 屬性會將目前 DateTimeOffset 物件的時間轉換為國際標準時間 (UTC) 。

  2. 屬性接著會將 UTC 轉換為當地時間。

沒有不正確時間,且模棱兩可的時間會對應到當地區域的標準時間。 不過, (轉換可能會建立異常:如果 DateTimeOffset 衍生自本機電腦的值反映模棱兩可的日期和時間,該值可以轉換成 UTC,然後回到與原始時間不同的當地時間。) 屬性在執行此轉換時,會在本機時區套用任何調整規則。

這個屬性會傳回 物件的日期和時間元件 DateTimeOffset ,這讓轉換很有用 DateTimeOffset DateTime 。 除了執行任何必要的時間轉換之外,此屬性與 屬性不同 DateTime ,方法是將 KindDateTime 回物件的 屬性值設定為 DateTimeKind.Local

適用於

另請參閱