C# TimeZoneInfo ConvertTime(DateTime, TimeZoneInfo, TimeZoneInfo)

Description

TimeZoneInfo ConvertTime(DateTime, TimeZoneInfo, TimeZoneInfo) converts a time from one time zone to another.

Syntax

TimeZoneInfo.ConvertTime(DateTime, TimeZoneInfo, TimeZoneInfo) has the following syntax.


public static DateTime ConvertTime(
  DateTime dateTime,/*  ww w . jav  a  2  s.  c  om*/
  TimeZoneInfo sourceTimeZone,
  TimeZoneInfo destinationTimeZone
)

Parameters

TimeZoneInfo.ConvertTime(DateTime, TimeZoneInfo, TimeZoneInfo) has the following parameters.

  • dateTime - The date and time to convert.
  • sourceTimeZone - The time zone of dateTime.
  • destinationTimeZone - The time zone to convert dateTime to.

Returns

TimeZoneInfo.ConvertTime(DateTime, TimeZoneInfo, TimeZoneInfo) method returns The date and time in the destination time zone that corresponds to the dateTime parameter in the source time zone.

Example

The following example illustrates the use of the ConvertTime(DateTime, TimeZoneInfo, TimeZoneInfo) method to convert from Hawaiian Standard Time to local time.


//from   w  ww.jav a 2 s  .c  o m
using System;
public class MainClass{
  public static void Main(String[] argv){  
   DateTime hwTime = new DateTime(2007, 02, 01, 08, 00, 00);

   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));

  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version