Is there a simple way to display Windows Time zone drop down in an application in C# so the user can change it?
Asked
Active
Viewed 734 times
2
-
You can set the timezone with `tzutil /s` or [via PInvoke](http://www.pinvoke.net/default.aspx/kernel32/SetTimeZoneInformation.html) – stuartd Sep 04 '20 at 21:10
1 Answers
4
I would do this as per the Microsoft Docs.
ReadOnlyCollection<TimeZoneInfo> tzCollection;
tzCollection = TimeZoneInfo.GetSystemTimeZones();
Then you could set a combobox DataSource equal to that collection like so
comboBox.DataSource = tzCollection;

Nick Juelich
- 428
- 2
- 13
-
It shows the UTC-12 as the default instead of the current time zone. How do I set it to current first and then make Windows accept the next timezone so the new timezone will be shown in Settings /Date and Time? – Bart Divanov Sep 04 '20 at 17:20
-
@BartDivanov This link should help you https://learn.microsoft.com/en-us/dotnet/api/system.timezoneinfo.local?view=netcore-3.1#System_TimeZoneInfo_Local – Nick Juelich Sep 04 '20 at 17:27
-
The link did not help, but this link did: https://stackoverflow.com/questions/9744707/adding-timezoneinfo-to-combobox-and-setting-seleteditem-baffles-me Thanks for your help! – Bart Divanov Sep 04 '20 at 17:44
-
I can set the comboBox to current timezone, but I do not see a way to set the system timezone based on the user's selection since TimeZoneInfo only gets information from the system date. Any suggestion on how to set the new timezone? – Bart Divanov Sep 04 '20 at 18:17
-
If you're just using the time zone in your application, then just hold on to the `TimeZoneInfo` object selected (or at least the `.Id` property). If you mean you want to set the system-wide time zone setting, then see [my answer here](https://stackoverflow.com/a/25153145/634824). – Matt Johnson-Pint Sep 04 '20 at 23:22
-
@MattJohnson-Pint I tried, but it did not work. Could this be that I supply the parameter taken from TimeZoneInfo as "(UTC-05:00) Havana" so when I used in SetSystemTimeZone() it does not do it? If so how do I get the name only from TimeZoneInfo ("Havana")? Note I take this string from a comboBox as shown above. – Bart Divanov Sep 05 '20 at 14:44
-
@MattJohnson-Pint Another thing: There is a switch in Settings/Date & Time that set timezone automatically. How this setting will effect if I use your method? – Bart Divanov Sep 05 '20 at 14:49
-
You should pass the `Id` - not the `DisplayName`. In a dropdown on the web, use the `DisplayName` to show to the user, and use the `Id` as the underlying value. In a Windows app, you can bind to the `TimeZoneInfo` object, but then pass the `Id` to set the time zone, not the object's string representation. – Matt Johnson-Pint Sep 05 '20 at 18:37
-
The automatic DST setting should almost never be disabled in modern Windows systems. The "dstoff" switch is an artifact from legacy days when we didn't have a complete list of time zones for the world. Don't use it. – Matt Johnson-Pint Sep 05 '20 at 18:38
-
@MattJohnson-Pint I need this for the desktop app (a wizard type app to setup the new machine), so I need to display the system timezones as they are shown in timedate.cpl app, not in the format of `TimeZoneInfo.Id` I use the following code to get timezone name from a combobox: `string tz_selected = this.cboTimeZone.GetItemText(this.cboTimeZone.SelectedItem);` How do I associate it with the `Id`? – Bart Divanov Sep 05 '20 at 22:44
-
@MattJohnson-Pint I agree, but I was asking about the _Set Time Zone Automatically_, not the _Adjust for DST Automatically_ switch. – Bart Divanov Sep 05 '20 at 23:01
-
Ah, ok - sorry, I misread your question. With regard to "Set Time Zone Automatically" - In most cases, that will do exactly what you want it to. It uses whatever location information is available to the computer (IP, Wi-Fi, GPS if equipped) to select the time zone for the computer's location. Works great when travelling. Not so much if you use a VPN that gets its Internet from another time zone. If your intention is to let the user pick a time zone from a list, you'll also want to disable automatic time zone, else you'll possible have the selection overridden. – Matt Johnson-Pint Sep 08 '20 at 17:07
-
You can do that by disabling the `tzautoupdate` Windows service. [See here for various ways to disable a Windows service](https://superuser.com/questions/460076/disable-a-windows-service-from-the-command-line). – Matt Johnson-Pint Sep 08 '20 at 17:08
-
As for the dropdown, you are binding to `TimeZoneInfo` objects. `GetItemText` is calling `ToString` on the selected object, which in-turn gives the `DisplayName` property (identical to `((TimeZoneInfo) this.cboTimeZone.SelectedItem).DisplayName`). So, instead, do `((TimeZoneInfo) this.cboTimeZone.SelectedItem).Id`. Casting is required because `SelectedItem` is typed as `object`. – Matt Johnson-Pint Sep 08 '20 at 17:13