Convert a time from one time zone to another based on time zone identifiers in CSharp

Description

The following code shows how to convert a time from one time zone to another based on time zone identifiers.

Example


/*  ww  w.jav  a  2s.  co m*/
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 »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var