Compartilhar via


DateTimeOffset.LocalDateTime Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a DateTime value that represents the local date and time of the current DateTimeOffset object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public ReadOnly Property LocalDateTime As DateTime
public DateTime LocalDateTime { get; }

Property Value

Type: System.DateTime
An object whose value represents the local date and time of the current DateTimeOffset object.

Remarks

If necessary, the LocalDateTime property converts the current DateTimeOffset object's date and time to the local system's date and time. The conversion is a two-step operation:

  1. The property converts the current DateTimeOffset object's time to Coordinated Universal Time (UTC).

  2. The property then converts UTC to local time.

There are no invalid times, and ambiguous times are mapped to the local zone's standard time. (However, the conversion can create an anomaly: if a DateTimeOffset value that is derived from the local computer reflects an ambiguous date and time, that value can be converted to UTC and then back to a local time that differs from the original time.) The property applies any adjustment rules in the local time zone when it performs this conversion.

This property returns both the date and the time component of a DateTimeOffset object, which makes it useful for DateTimeOffset to DateTime conversion. In addition to performing any necessary time conversion, this property differs from the DateTime property by setting the value of the Kind property of the returned DateTime object to DateTimeKind.Local.

Examples

The following example illustrates several conversions of DateTimeOffset values to local times in the U.S. Pacific Standard Time zone. Note that the last three times are all ambiguous; the property maps all of them to a single date and time in the Pacific Standard Time zone.

Dim dto As DateTimeOffset

' Current time
dto = DateTimeOffset.Now
outputBlock.Text &= dto.LocalDateTime & vbCrLf
' UTC time
dto = DateTimeOffset.UtcNow
outputBlock.Text &= dto.LocalDateTime & vbCrLf

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

' 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:30:00 AM#, New TimeSpan(-7, 0, 0))
outputBlock.Text &= TimeZoneInfo.Local.IsAmbiguousTime(dto) & vbCrLf
outputBlock.Text &= dto.LocalDateTime & vbCrLf
dto = New DateTimeOffset(#11/4/2007 2:30:00 AM#, New TimeSpan(-7, 0, 0))
outputBlock.Text &= TimeZoneInfo.Local.IsAmbiguousTime(dto) & vbCrLf
outputBlock.Text &= dto.LocalDateTime & vbCrLf
' This is also an ambiguous time
dto = New DateTimeOffset(#11/4/2007 1:30:00 AM#, New TimeSpan(-8, 0, 0))
outputBlock.Text &= TimeZoneInfo.Local.IsAmbiguousTime(dto) & vbCrLf
outputBlock.Text &= dto.LocalDateTime & vbCrLf
' 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      
DateTimeOffset dto;

// Current time
dto = DateTimeOffset.Now;
outputBlock.Text += dto.LocalDateTime + "\n";
// UTC time
dto = DateTimeOffset.UtcNow;
outputBlock.Text += dto.LocalDateTime + "\n";

// 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));
outputBlock.Text += dto.LocalDateTime + "\n";
dto = new DateTimeOffset(2007, 3, 11, 2, 30, 0, new TimeSpan(-7, 0, 0));
outputBlock.Text += dto.LocalDateTime + "\n";
// Invalid time in local time zone
dto = new DateTimeOffset(2007, 3, 11, 2, 30, 0, new TimeSpan(-8, 0, 0));
outputBlock.Text += TimeZoneInfo.Local.IsInvalidTime(dto.DateTime) + "\n";
outputBlock.Text += dto.LocalDateTime + "\n";

// 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));
outputBlock.Text += TimeZoneInfo.Local.IsAmbiguousTime(dto) + "\n";
outputBlock.Text += dto.LocalDateTime + "\n";
dto = new DateTimeOffset(2007, 11, 4, 2, 30, 0, new TimeSpan(-7, 0, 0));
outputBlock.Text += TimeZoneInfo.Local.IsAmbiguousTime(dto) + "\n";
outputBlock.Text += dto.LocalDateTime + "\n";
// This is also an ambiguous time
dto = new DateTimeOffset(2007, 11, 4, 1, 30, 0, new TimeSpan(-8, 0, 0));
outputBlock.Text += TimeZoneInfo.Local.IsAmbiguousTime(dto) + "\n";
outputBlock.Text += dto.LocalDateTime + "\n";
// 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      

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.