Aracılığıyla paylaş


DateTimeOffset.LocalDateTime Özellik

Tanım

Geçerli DateTimeOffset nesnenin yerel tarihini ve saatini temsil eden bir DateTime değer alır.

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

Özellik Değeri

Geçerli DateTimeOffset nesnenin yerel tarihi ve saati.

Örnekler

Aşağıdaki örnek, değerlerin DateTimeOffset ABD Pasifik Standart Saat dilimindeki yerel saatlere çeşitli dönüştürmelerini göstermektedir. Son üç kez tamamen belirsiz olduğunu unutmayın; özelliği, bunların tümünü Pasifik Standart Saat dilimindeki tek bir tarih ve saatle eşler.

 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

Açıklamalar

Gerekirse, LocalDateTime özelliği geçerli DateTimeOffset nesnenin tarih ve saatini yerel sistemin tarih ve saatine dönüştürür. Dönüştürme iki adımlı bir işlemdir:

  1. özelliği geçerli DateTimeOffset nesnenin saatini Eşgüdümlü Evrensel Saat'e (UTC) dönüştürür.

  2. Ardından özelliği UTC'yi yerel saate dönüştürür.

Geçersiz saat yok ve belirsiz saatler yerel bölgenin standart saatiyle eşlenir. (Ancak, dönüştürme bir anomali oluşturabilir: Yerel bilgisayardan türetilen bir DateTimeOffset değer belirsiz bir tarih ve saati yansıtıyorsa, bu değer UTC'ye dönüştürülebilir ve ardından özgün saatten farklı bir yerel saate geri döndürülebilir.) özelliği, bu dönüştürmeyi gerçekleştirirken yerel saat dilimindeki tüm ayarlama kurallarını uygular.

Bu özellik, bir DateTimeOffset nesnenin hem tarih hem de saat bileşenini döndürür ve bu da dönüştürme için DateTimeOffsetDateTime yararlı olmasını sağlar. Bu özellik, gerekli zaman dönüştürme işlemlerini gerçekleştirmeye ek olarak, döndürülen DateTime nesnenin DateTimeDateTimeKind.Localözelliğinin Kind değerini olarak ayarlayarak özelliğinden farklıdır.

Şunlara uygulanır

Ayrıca bkz.