Export (0) Print
Expand All

TimeZone.ToLocalTime Method

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
)
public DateTime ToLocalTime (
	DateTime time
)
public function ToLocalTime (
	time : DateTime
) : DateTime
Not applicable.

Parameters

time

A Coordinated Universal Time (UTC) time.

Return Value

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.

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 System.TimeZoneInfo.ConvertTimeFromUtc(System.DateTime,System.TimeZoneInfo) method with its destinationTimeZone parameter set to System.TimeZoneInfo.Local.

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.");
   }      
}

The following example uses the ToLocalTime method to return the local times that correspond to several Coordinated Universal Time (UTC) times.

// Example of the TimeZone.ToLocalTime( DateTime ) and 
// TimeZone.GetUtcOffset( DateTime ) methods.
using System;

class UTCTimeDemo
{
    static void Main( )
    {
        const string headFmt = "{0,-20}{1,-20}{2,-12}{3}";

        // Get the local time zone and a base Coordinated Universal 
        // Time (UTC).
        TimeZone localZone = TimeZone.CurrentTimeZone;
        DateTime baseUTC = new DateTime( 2000, 1, 1 );

        Console.WriteLine( "This example of \n" +
            "   TimeZone.ToLocalTime( DateTime ) and\n" +
            "   TimeZone.GetUtcOffset( DateTime ) \ngenerates the " +
            "following output, which varies depending on the time " +
            "zone \nin which it is run. The example creates several " +
            "Coordinated Universal \nTimes (UTC), displays the " +
            "corresponding local times and UTC offsets, \nand shows " +
            "if the times occur in daylight saving time (DST)." );
        Console.WriteLine( "\nLocal time: {0}\n", 
            localZone.StandardName );

        Console.WriteLine( headFmt, "UTC", "Local Time", 
            " Offset", "DST?" );
        Console.WriteLine( headFmt, "---", "----------", 
            " ------", "----" );

        // Generate several UTC times.
        for( int loopX = 0; loopX <= 10; loopX++ )
        {
            // Calculate the local time and UTC offset.
            DateTime localTime = localZone.ToLocalTime( baseUTC );
            TimeSpan localOffset = 
                localZone.GetUtcOffset( localTime );

            Console.WriteLine( "{0,-20:yyyy-MM-dd HH:mm}" +
                "{1,-20:yyyy-MM-dd HH:mm}{2,-12}{3}", 
                baseUTC, localTime, localOffset, 
                localZone.IsDaylightSavingTime( localTime ) );
            
            // Advance to another UTC.
            baseUTC = baseUTC.AddDays( 155.55 );
        }
    } 
} 

/*
This example of
   TimeZone.ToLocalTime( DateTime ) and
   TimeZone.GetUtcOffset( DateTime )
generates the following output, which varies depending on the time zone
in which it is run. The example creates several Coordinated Universal
Times (UTC), displays the corresponding local times and UTC offsets,
and shows if the times occur in daylight saving time (DST).

Local time: Pacific Standard Time

UTC                 Local Time           Offset     DST?
---                 ----------           ------     ----
2000-01-01 00:00    1999-12-31 16:00    -08:00:00   False
2000-06-04 13:12    2000-06-04 06:12    -07:00:00   True
2000-11-07 02:24    2000-11-06 18:24    -08:00:00   False
2001-04-11 15:36    2001-04-11 08:36    -07:00:00   True
2001-09-14 04:48    2001-09-13 21:48    -07:00:00   True
2002-02-16 18:00    2002-02-16 10:00    -08:00:00   False
2002-07-22 07:12    2002-07-22 00:12    -07:00:00   True
2002-12-24 20:24    2002-12-24 12:24    -08:00:00   False
2003-05-29 09:36    2003-05-29 02:36    -07:00:00   True
2003-10-31 22:48    2003-10-31 14:48    -08:00:00   False
2004-04-04 12:00    2004-04-04 05:00    -07:00:00   True
*/ 

// Example of the TimeZone.ToLocalTime( DateTime ) and 
// TimeZone.GetUtcOffset( DateTime ) methods.
import System.*;

class UTCTimeDemo
{
    public static void main(String[] args)
    {
        final String headFmt = "{0,-20}{1,-20}{2,-12}{3}";

        // Get the local time zone and a base Coordinated Universal 
        // Time (UTC).
        TimeZone localZone = TimeZone.get_CurrentTimeZone();
        DateTime baseUTC = new DateTime(2000, 1, 1);
        Console.WriteLine(("This example of \n" 
            + "TimeZone.ToLocalTime( DateTime ) and\n" 
            + "   TimeZone.GetUtcOffset( DateTime ) \ngenerates the " 
            + "following output, which varies depending on the time " 
            + "zone \nin which it is run. The example creates several " 
            + "Coordinated Universal \nTimes (UTC), displays the " 
            + "corresponding local times and UTC offsets, \nand shows " 
            + "if the times occur in daylight saving time (DST)."));
        Console.WriteLine("\nLocal time: {0}\n",localZone.get_StandardName());
        Console.WriteLine(headFmt, new Object[] { "UTC", "Local Time",
            " Offset","DST?" });
        Console.WriteLine(headFmt, new Object[] { "---", "----------",
            " ------", "----" });

        // Generate several UTC times.
        for (int loopX = 0; loopX <= 10; loopX++) {
            // Calculate the local time and UTC offset.
            DateTime localTime = localZone.ToLocalTime(baseUTC);
            TimeSpan localOffset = localZone.GetUtcOffset(localTime);

            Console.WriteLine("{0,-20:yyyy-MM-dd HH:mm}" 
                +    "{1,-20:yyyy-MM-dd HH:mm}{2,-12}{3}",
                new Object[] {baseUTC, localTime, localOffset,
                (System.Boolean)localZone.IsDaylightSavingTime(localTime) });

            // Advance to another UTC.
            baseUTC = baseUTC.AddDays(155.55);
        }
    } //main
} //UTCTimeDemo

/*
This example of
   TimeZone.ToLocalTime( DateTime ) and
   TimeZone.GetUtcOffset( DateTime )
generates the following output, which varies depending on the time zone
in which it is run. The example creates several Coordinated Universal
Times (UTC), displays the corresponding local times and UTC offsets,
and shows if the times occur in daylight saving time (DST).

Local time: Pacific Standard Time

UTC                 Local Time           Offset     DST?
---                 ----------           ------     ----
2000-01-01 00:00    1999-12-31 16:00    -08:00:00   False
2000-06-04 13:12    2000-06-04 06:12    -07:00:00   True
2000-11-07 02:24    2000-11-06 18:24    -08:00:00   False
2001-04-11 15:36    2001-04-11 08:36    -07:00:00   True
2001-09-14 04:48    2001-09-13 21:48    -07:00:00   True
2002-02-16 18:00    2002-02-16 10:00    -08:00:00   False
2002-07-22 07:12    2002-07-22 00:12    -07:00:00   True
2002-12-24 20:24    2002-12-24 12:24    -08:00:00   False
2003-05-29 09:36    2003-05-29 02:36    -07:00:00   True
2003-10-31 22:48    2003-10-31 14:48    -08:00:00   False
2004-04-04 12:00    2004-04-04 05:00    -07:00:00   True
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft