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






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

1 comment:

manishvtr said...

DateTime.UtcNow.AddHours(5.5).ToString()