TimeZone Custom c# Class

by Otto on February 6, 2007

I found this great c# class last night, something that has been missing in .NET for too long.  A TimeZone conversion utility.  Convert from one timezone to another very easily.  The c# class was written by William Stacy in December 2005.  With a little tweaking for .NET 2.0, I have the class working.  Here is a screenshot listing all the timezones listed in a gridview.

You can download the updated c# class here:  TimeZoneCustom.txt (15.56 KB)

The original blog post didn’t have much in the way of code samples and I don’t plan on doing much more, but I’ll give you these.

// Get all time zone names
string[] allTimeZones = TimeZoneInformation.TimeZoneNames;

// Get current time zone of this computer
TimeZoneInformation t = TimeZoneInformation.CurrentTimeZone;

// Get a list of all the time zones
List<TimeZoneInformation> list = TimeZoneInformation.TimeZones;

// Sort the list by Standard Bias
list.Sort(delegate(TimeZoneInformation t1, TimeZoneInformation t2) {return Comparer<int>.Default.Compare(t1.StandardBias, t2.StandardBias); });

// Display the timezones in a gridview
GridView1.DataSource = list;
GridView1.DataBind();

If you need more examples, let me know.  Also, I typically use Web Projects in Visual Studio 2005, which means this class will need to be wrapped in a namespace if you want to use it in standard projects.

{ 2 comments }

Olga Tsitlik June 16, 2008 at 8:58 am

Can I have more examples (TimeZone Custom c# Class).Thanks

Chris June 16, 2008 at 8:58 am

Looks pretty sweet – thanks!

Comments on this entry are closed.

Previous post:

Next post: