Export (0) Print
Expand All

TimeZoneInfo.ConvertTimeBySystemTimeZoneId Method (DateTime, String, String)

Updated: April 2009

Converts a time from one time zone to another based on time zone identifiers.

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

public static DateTime ConvertTimeBySystemTimeZoneId(
	DateTime dateTime,
	string sourceTimeZoneId,
	string destinationTimeZoneId
)

Parameters

dateTime
Type: System.DateTime

The date and time to convert.

sourceTimeZoneId
Type: System.String

The identifier of the source time zone.

destinationTimeZoneId
Type: System.String

The identifier of the destination time zone.

Return Value

Type: System.DateTime
A DateTime value that represents the date and time in the destination time zone that corresponds to the dateTime parameter in the source time zone.

ExceptionCondition
ArgumentException

The Kind property of the dateTime parameter does not correspond to the source time zone.

-or-

dateTime is an invalid time in the source time zone.

ArgumentNullException

sourceTimeZoneId is null.

-or-

destinationTimeZoneId is null.

InvalidTimeZoneException

The time zone identifier was found, but the registry data is corrupted.

SecurityException

The process does not have the permissions required to read from the registry key that contains the time zone information.

TimeZoneNotFoundException

The sourceTimeZoneId identifier was not found on the local system.

-or-

The destinationTimeZoneId identifier was not found on the local system.

SecurityException

The user does not have the permissions required to read from the registry keys that hold time zone data.

When performing the conversion, the ConvertTimeBySystemTimeZoneId method applies any adjustment rules in effect in the destinationTimeZoneId time zone.

Although it is similar to the TimeZoneInfo.ConvertTime(DateTime, TimeZoneInfo, TimeZoneInfo) method, you can use TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime, String, String) to specify the source and destination time zones using their identifiers instead of their TimeZoneInfo objects. This method is most useful when you must convert a time without retrieving the time zone object that corresponds to it and you do not need to know whether the converted time is standard or daylight saving time.

This method retrieves the time zones whose identifiers are the sourceTimeZoneId and destinationTimeZoneId parameters from the registry. It cannot retrieve time zone objects that are created using the CreateCustomTimeZone method.

The value of the Kind property of the dateTime parameter must correspond to the sourceTimeZoneId parameter, as the following table shows.

DateTime.Kind value

sourceTimeZone value

Method behavior

DateTimeKind.Utc

Equals TimeZoneInfo.Utc.Id.

Converts dateTime to the destination time zone's time.

DateTimeKind.Utc

Does not equal TimeZoneInfo.Utc.Id.

Throws an ArgumentException.

DateTimeKind.Local

Equals TimeZoneInfo.Local.Id.

Converts dateTime to the destination time zone's time.

DateTimeKind.Local

Does not equal TimeZoneInfo.Local.Id.

Throws an ArgumentException.

DateTimeKind.Unspecified

Any.

Converts dateTime to the destination time zone's time.

Because it relies on calls to the FindSystemTimeZoneById method, the ConvertTimeBySystemTimeZoneId method performs a case-insensitive search to locate the time zones that correspond to sourceTimeZoneId and destinationTimeZoneId.

If the value of the dateTime parameter is an ambiguous time in the source time zone, it is interpreted as a standard time. If the dateTime parameter is an invalid time in the source time zone, this method throws an ArgumentException.

The Kind property of the returned DateTime value is set to DateTimeKind.Unspecified unless the destination time zone is Coordinated Universal Time (UTC), in which case it is set to DateTimeKind.Utc.

The following example uses the TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime, String, String) method to display the time that corresponds to the local system time in eight cities of the world.

DateTime currentTime = DateTime.Now;
Console.WriteLine("Current Times:");
Console.WriteLine();
Console.WriteLine("Los Angeles: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Pacific Standard Time"));
Console.WriteLine("Chicago: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Central Standard Time"));
Console.WriteLine("New York: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Eastern Standard Time"));
Console.WriteLine("London: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "GMT Standard Time"));
Console.WriteLine("Moscow: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Russian Standard Time"));
Console.WriteLine("New Delhi: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "India Standard Time"));
Console.WriteLine("Beijing: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "China Standard Time"));
Console.WriteLine("Tokyo: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Tokyo Standard Time"));

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