Edit

Bagikan melalui


TimeZone.ToLocalTime(DateTime) Method

Definition

Returns the local time that corresponds to a specified date and time value.

public:
 virtual DateTime ToLocalTime(DateTime time);
public virtual DateTime ToLocalTime (DateTime time);
abstract member ToLocalTime : DateTime -> DateTime
override this.ToLocalTime : DateTime -> DateTime
Public Overridable Function ToLocalTime (time As DateTime) As DateTime

Parameters

time
DateTime

A Coordinated Universal Time (UTC) time.

Returns

A DateTime object whose value is the local time that corresponds to time.

Remarks

The following table shows the relationship between the time parameter and the DateTime value returned by this method.

time parameter Behavior Return value
A Coordinated Universal Time (UTC) time (DateTimeKind.Utc). Converts the time from UTC to the local time. A DateTime object whose value is the local time that corresponds to time.
A local time (DateTimeKind.Local). No conversion necessary. The same DateTime value represented by the time parameter.
An unspecified time (DateTimeKind.Unspecified). Assumes that the time is UTC and converts it from UTC to the local time. A DateTime object whose value is the local time that corresponds to time.

If the local time zone observes daylight saving time, ToLocalTime applies the current adjustment rule to time when performing the conversion.

Note

The ToLocalTime method recognizes only the current daylight saving time adjustment rule for the local time zone. As a result, it is guaranteed to accurately return the local time corresponding to a particular UTC time only during the period in which the latest adjustment rule is in effect. It may return inaccurate results if time is a historic date and time value that was subject to a previous adjustment rule.

The ToLocalTime method corresponds to the TimeZoneInfo.ConvertTimeFromUtc method with its destinationTimeZone parameter set to TimeZoneInfo.Local. Whenever possible, use the TimeZoneInfo.ConvertTimeFromUtc method.

Notes to Inheritors

Although it is not required, in most cases derived classes running under the .NET Framework version 2.0 should override the default implementation of this method. In the .NET Framework versions 1.0 and 1.1, the ToLocalTime method called the GetUtcOffset(DateTime) method and adjusted for daylight saving time when returning the local time. However, starting with the .NET Framework 2.0, the behavior of the default implementation depends on the Kind property of the time parameter. If its value is Local, this method returns time unchanged. If its value is either Utc or Unspecified, this method assumes time is UTC and converts it to the local system time without calling the GetUtcOffset(DateTime) method.

The following code provides a simple override of the default implementation of the ToLocalTime method. In this code, the internalTimeZone variable represents a private instance of the TimeZone class:

public override DateTime ToLocalTime(DateTime time)
{
   if (time.Kind == DateTimeKind.Local)
   {
      return time;
   }
   else if (time.Kind == DateTimeKind.Utc)
   {
      DateTime returnTime = new DateTime(time.Ticks, DateTimeKind.Local);
      returnTime += this.GetUtcOffset(returnTime);
      if (internalTimeZone.IsDaylightSavingTime(returnTime))
         returnTime -= new TimeSpan(1, 0, 0);
      return returnTime;
   }      
   else
   {
      throw new ArgumentException("The source time zone cannot be determined.");
   }      
}
Public Overrides Function ToLocalTime(time As Date) As Date
   If time.Kind = DateTimeKind.Local Then
      Return time
   ElseIf time.Kind = DateTimeKind.Utc Then
      Dim returnTime As New Date(time.Ticks, DateTimeKind.Local)
      returnTime += me.GetUtcOffset(returnTime)
      if internalTimeZone.IsDaylightSavingTime(returnTime) Then
         returnTime -= New TimeSpan(1, 0, 0)
      End If
      Return returnTime      
   Else
      Throw New ArgumentException("The source time zone cannot be determined.")
   End If      
End Function

Applies to

See also