TimeZoneInfo.ConvertTimeFromUtc(DateTime, TimeZoneInfo) Method

Definition

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

C#
public static DateTime ConvertTimeFromUtc(DateTime dateTime, TimeZoneInfo destinationTimeZone);

Parameters

dateTime
DateTime

The Coordinated Universal Time (UTC).

destinationTimeZone
TimeZoneInfo

The time zone to convert dateTime to.

Returns

The date and time in the destination time zone. Its Kind property is Utc if destinationTimeZone is Utc; otherwise, its Kind property is Unspecified.

Exceptions

The Kind property of dateTime is Local.

destinationTimeZone is null.

Examples

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

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

Remarks

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.

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also