Converts a time from one time zone to another.
Namespace:
System
Assembly:
System.Core (in System.Core.dll)
Visual Basic (Declaration)
Public Shared Function ConvertTime ( _
dateTime As DateTime, _
sourceTimeZone As TimeZoneInfo, _
destinationTimeZone As TimeZoneInfo _
) As DateTime
Dim dateTime As DateTime
Dim sourceTimeZone As TimeZoneInfo
Dim destinationTimeZone As TimeZoneInfo
Dim returnValue As DateTime
returnValue = TimeZoneInfo.ConvertTime(dateTime, _
sourceTimeZone, destinationTimeZone)
public static DateTime ConvertTime(
DateTime dateTime,
TimeZoneInfo sourceTimeZone,
TimeZoneInfo destinationTimeZone
)
public:
static DateTime ConvertTime(
DateTime dateTime,
TimeZoneInfo^ sourceTimeZone,
TimeZoneInfo^ destinationTimeZone
)
public static function ConvertTime(
dateTime : DateTime,
sourceTimeZone : TimeZoneInfo,
destinationTimeZone : TimeZoneInfo
) : DateTime
Return Value
Type:
System..::.DateTimeA DateTime value that represents the date and time in the destination time zone that corresponds to the dateTime parameter in the source time zone.
| Exception | Condition |
|---|
| ArgumentException | The Kind property of the dateTime parameter is DateTimeKind..::.Local, but the sourceTimeZone parameter does not equal DateTimeKind..::.Local. -or- The Kind property of the dateTime parameter is DateTimeKind..::.Utc, but the sourceTimeZone parameter does not equal TimeZoneInfo..::.Utc. -or- The dateTime parameter is an invalid time (that is, it represents a time that does not exist because of a time zone's adjustment rules). |
| ArgumentNullException | The sourceTimeZone parameter is nullNothingnullptra null reference (Nothing in Visual Basic). -or- The destinationTimeZone parameter is nullNothingnullptra null reference (Nothing in Visual Basic). |
The value of the Kind property of the dateTime parameter must correspond to the sourceTimeZone parameter, as the following table shows.
You can also convert to or from Coordinated Universal Time (UTC) by calling the ConvertTimeFromUtc and ConvertTimeToUtc methods.
The Kind property of the returned DateTime value is set as shown in the following table.
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.
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 illustrates the use of the ConvertTime(DateTime, TimeZoneInfo, TimeZoneInfo) method to convert from Hawaiian Standard Time to local time.
Dim hwTime As Date = #2/01/2007 8:00:00 AM#
Try
Dim hwZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time")
Console.WriteLine("{0} {1} is {2} local time.", _
hwTime, _
IIf(hwZone.IsDaylightSavingTime(hwTime), hwZone.DaylightName, hwZone.StandardName), _
TimeZoneInfo.ConvertTime(hwTime, hwZone, TimeZoneInfo.Local))
Catch e As TimeZoneNotFoundException
Console.WriteLine("The registry does not define the Hawaiian Standard Time zone.")
Catch e As InvalidTimeZoneException
Console.WriteLine("Registry data on the Hawaiian Standard Time zone has been corrupted.")
End Try
DateTime hwTime = new DateTime(2007, 02, 01, 08, 00, 00);
try
{
TimeZoneInfo hwZone = TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time");
Console.WriteLine("{0} {1} is {2} local time.",
hwTime,
hwZone.IsDaylightSavingTime(hwTime) ? hwZone.DaylightName : hwZone.StandardName,
TimeZoneInfo.ConvertTime(hwTime, hwZone, TimeZoneInfo.Local));
}
catch (TimeZoneNotFoundException)
{
Console.WriteLine("The registry does not define the Hawaiian Standard Time zone.");
}
catch (InvalidTimeZoneException)
{
Console.WriteLine("Registry data on the Hawaiian 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
Reference
Other Resources