开发者

How to order a TimeZone list in .NET?

开发者 https://www.devze.com 2022-12-20 06:24 出处:网络
I have an ASP.NET web application that requires users to select their appropriate time zone so that it can correctly show local times for events.

I have an ASP.NET web application that requires users to select their appropriate time zone so that it can correctly show local times for events.

In creating a simple approach for selecting the time zone, I started by just using the values from TimeZoneInfo.GetSys开发者_运维技巧temTimeZones(), and showing that list.

The only problem with this is that since our application is primarily targeted at the United States, I'd like to show those entries first, basically starting with Eastern Time and working backwards (West) until I reach Atlantic time.

What's a good approach to do this in code?


Using LINQ:

var timezones = TimeZoneInfo.GetSystemTimeZones();
var orderedZones = (from t in timezones
                                where t.DisplayName.Contains("(US & Canada)")
                                select t).Concat(
                                from t in timezones 
                                where t.DisplayName.Contains("(US & Canada)") == false
                                select t
                               );


Ok, so my solution: I put the display name, Id, and the order I wanted them in the database. It works, as I just store the Id in a user object anyway. I'll grab it once, and store it in the application cache.

Hack-y, but it "solves" the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消