TimeZone.ToLocalTime Method (DateTime)

 

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

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

public virtual DateTime ToLocalTime(
	DateTime time
)

Parameters

time
Type: System.DateTime

A Coordinated Universal Time (UTC) time.

Return Value

Type: System.DateTime

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

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.

System_CAPS_noteNote

   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 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 DateTimeKind.Local, this method returns time unchanged. If its value is either DateTimeKind.Utc or DateTimeKind.Unspecified, this method assumes time is UTC and converts it to the local system time without calling the GetUtcOffset 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.");
   }      
}

.NET Framework
Available since 1.1
Return to top
Show: