C# TimeZoneInfo ConvertTimeBySystemTimeZoneId(DateTime, String, String)

Description

TimeZoneInfo ConvertTimeBySystemTimeZoneId(DateTime, String, String) converts a time from one time zone to another based on time zone identifiers.

Syntax

TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime, String, String) has the following syntax.


public static DateTime ConvertTimeBySystemTimeZoneId(
  DateTime dateTime,/*from   ww  w .j  av  a 2 s .  c  o  m*/
  string sourceTimeZoneId,
  string destinationTimeZoneId
)

Parameters

TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime, String, String) has the following parameters.

  • dateTime - The date and time to convert.
  • sourceTimeZoneId - The identifier of the source time zone.
  • destinationTimeZoneId - The identifier of the destination time zone.

Returns

TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime, String, String) 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 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.


//from  w  ww  .  ja va 2s  .  c  om
using System;
public class MainClass{
  public static void Main(String[] argv){  
    DateTime currentTime = DateTime.Now;
    
    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"));
    

  }
}
    

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