Skip to main content

How to create a list of time zones programmatically

It's very easy to create a list of timezones! And that is very useful too!!

Posted in C#, Code snippets

Imagine that you are developing an application which requires the user to choose in which time zone he is located. Now picture yourself adding 40 time zones into a dropdownlist manually.

Unless you need to have the list of time zones in the database you can just use the following code snippet:

public static IEnumerable<SelectListItem> TimeZoneCollection
		return new SelectList(TimeZoneInfo.GetSystemTimeZones()
			.Select(q => new SelectListItem
							Text = q.DisplayName,
							Value = (q.DisplayName.Substring(0, 5) != "(UTC)") ? q.DisplayName.Substring(4, 3) : "0"
				    ), "Value""Text");

Now just bind it into a dropdownlist and voilà.

P.S.: Let me know in case you have improved the code. :)

Did you like the article?

You can subscribe and get them as soon they are online, share using the buttons bellow or leave a comment.

And you also can share using your favorite social network: