Convert DateTime to Different TimeZones

The TimeZoneInfo.ConvertTimeBySystemTimeZoneId in .NET 3.5 and 4.0, converts a time to the time in another time zone, based on a time zone identifier. Here’s how to use this method using different time zone identifiers like “India Standard Time”, “Pacific Standard Time” and so on:

C#

static void Main(string[] args)
{
DateTime dt = DateTime.Now;
Console.WriteLine("Time in Different TimeZones:\n");

Console.WriteLine("Indian Standard Time (IST): {0}",
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt,
TimeZoneInfo.Local.Id, "India Standard Time"));
Console.WriteLine("Eastern Standard Time (EST): {0}",
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt,
TimeZoneInfo.Local.Id, "Eastern Standard Time"));
Console.WriteLine("Pacific Standard Time (PST): {0}",
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt,
TimeZoneInfo.Local.Id, "Pacific Standard Time"));
Console.WriteLine("Greenwich Mean Time (GMT): {0}",
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt,
TimeZoneInfo.Local.Id, "GMT Standard Time"));
Console.ReadLine();
}

VB.NET (Converted Code)



Imports Microsoft.VisualBasic

Sub Main()
Dim dt As Date = Date.Now
Console.WriteLine("Time in Different TimeZones:" & vbLf)

Console.WriteLine("Indian Standard Time (IST): {0}", TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt, TimeZoneInfo.Local.Id, "India Standard Time"))
Console.WriteLine("Eastern Standard Time (EST): {0}", TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt, TimeZoneInfo.Local.Id, "Eastern Standard Time"))
Console.WriteLine("Pacific Standard Time (PST): {0}", TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt, TimeZoneInfo.Local.Id, "Pacific Standard Time"))
Console.WriteLine("Greenwich Mean Time (GMT): {0}", TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt, TimeZoneInfo.Local.Id, "GMT Standard Time"))
Console.ReadLine()
End Sub



OUTPUT

image

1 comment: