Export (0) Print
Expand All

TimeZoneInfo.ConvertTimeFromUtc Method

Updated: April 2009

Converts a Coordinated Universal Time (UTC) to the time in a specified time zone.

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

public static DateTime ConvertTimeFromUtc(
	DateTime dateTime,
	TimeZoneInfo destinationTimeZone
)

Parameters

dateTime
Type: System.DateTime

The Coordinated Universal Time (UTC).

destinationTimeZone
Type: System.TimeZoneInfo

The time zone to convert dateTime to.

Return Value

Type: System.DateTime
A DateTime value that represents the date and time in the destination time zone. Its DateTime.Kind property is DateTimeKind.Utc if destinationTimeZone is TimeZoneInfo.Utc; otherwise, its DateTime.Kind property is DateTimeKind.Unspecified.

ExceptionCondition
ArgumentException

The Kind property of dateTime is DateTimeKind.Local.

ArgumentNullException

destinationTimeZone is null.

When performing the conversion, the ConvertTimeFromUtc method applies any adjustment rules in effect in the destinationTimeZone time zone.

The precise behavior of this method depends on the value of the Kind property of the dateTime parameter, as the following table shows.

DateTime.Kind property

Conversion

DateTimeKind.Local

Throws an ArgumentException.

DateTimeKind.Unspecified or DateTimeKind.Utc

Converts from Coordinated Universal Time (UTC).

If the conversion of dateTime results in a date and time value that is earlier than DateTime.MinValue or later than DateTime.MaxValue, this method returns DateTime.MinValue or DateTime.MaxValue, respectively.

The following example converts Coordinated Universal Time (UTC) to Central Standard Time.

DateTime timeUtc = DateTime.UtcNow;
try
{
   TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
   DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);
   Console.WriteLine("The date and time are {0} {1}.", 
                     cstTime, 
                     cstZone.IsDaylightSavingTime(cstTime) ?
                             cstZone.DaylightName : cstZone.StandardName);
}
catch (TimeZoneNotFoundException)
{
   Console.WriteLine("The registry does not define the Central Standard Time zone.");
}                           
catch (InvalidTimeZoneException)
{
   Console.WriteLine("Registry data on the Central STandard Time zone has been corrupted.");
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5

Date

History

Reason

April 2009

Added exception information.

Information enhancement.

Community Additions

ADD
Show:
© 2014 Microsoft