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



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 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()