全球化问题:系统语言非英文时如何转换TimeZone.CurrentTimeZone.StandardName为英文?
Got it, let's tackle this issue. The problem with TimeZone.CurrentTimeZone.StandardName not respecting your CurrentUICulture setting is actually tied to how that old API works—let me break this down and give you actionable fixes.
First, why the CurrentUICulture switch doesn't help:
The legacy
TimeZoneclass pulls the standard name directly from the system's localized timezone database, which is tied to the system's default language/region settings—not the thread'sCurrentUICulture. ChangingCurrentUICulturewon't override this because this API wasn't designed to respect runtime culture switches for timezone names.
Recommended Solution: Use TimeZoneInfo (Modern API)
The TimeZoneInfo class replaces the outdated TimeZone and offers proper globalization support for timezone names. Here's how to get the English standard name:
- Grab the local timezone's
TimeZoneInfoinstance - Use
GetDisplayName()with the English culture explicitly specified
using System; using System.Globalization; public static string GetEnglishTimeZoneStandardName() { var localZone = TimeZoneInfo.Local; var enUsCulture = CultureInfo.GetCultureInfo("en-US"); // Get the standard name in English return localZone.GetDisplayName( TimeZoneInfoDisplayType.StandardName, enUsCulture ); }
Compatibility with Legacy TimeZone Code
If you're stuck maintaining code that uses TimeZone and can't switch to TimeZoneInfo entirely, you can map the old TimeZone to its TimeZoneInfo equivalent and fetch the English name that way:
var oldTimeZone = TimeZone.CurrentTimeZone; var correspondingZone = TimeZoneInfo.FindSystemTimeZoneById(oldTimeZone.Id); var enUsCulture = CultureInfo.GetCultureInfo("en-US"); string englishStandardName = correspondingZone.GetDisplayName( TimeZoneInfoDisplayType.StandardName, enUsCulture );
Key Notes
- This works across .NET Framework, .NET Core, and .NET 5+
- Most modern OSes (Windows, Linux, macOS) include multi-language timezone metadata by default, so you won't need extra installations
- Avoid relying on
TimeZonefor new development—it's marked as obsolete in .NET 6+ due to limited globalization support
内容的提问来源于stack exchange,提问作者yBother

