Reputation: 530
I have a small web application (.Net Framwrok 3.5, ASP) where the user sets the date time to an action be automatically performed on the server but recently i discovered that since the server and the user have different time zones, when the user picks a time to perform an action, it gets done on the server side one hour later than the sheduled one.
This is caused by the diference in timezone from the server and the client.
Im looking for the best approach to solve this and im considering the next options:
1.- Give, next to the time input, a drop down list of time zones(Ex: UTC +1 Bursels, Copenhage,paris; UTC+2.. Etc). Is there any way to programmatically get this list or must code it by myself?
2.- Do the same but on some kind of profile page i have, this way I could store the value but may have the case that the user never takes care of setting the proper value.
3.- Get the time offset from the user due some browser script or cookie, this is the best approach I found about it but I'm unsure about it's reliability: http://www.codeproject.com/Questions/107174/How-to-get-client-machine-time-zone
4.- Is there any way to have it by any simple C# instruction?
5.- Do nothing at all, it's an option, not the best since this kind of details does matter but I don't discard it.
Upvotes: 2
Views: 2003
Reputation: 241450
There are many ways to approach this problem. Some which involve JavaScript, some which involve .NET libraries like Noda Time, and some that are built in to the framework.
Probably the simplest thing for you to do would be to use the built-in TimeZoneInfo
class. You can create a drop-down list of time zones by calling TimeZoneInfo.GetSystemTimeZones
. You would use the Id
and DisplayName
properties to build the dropdown, and store the selected Id
to use for conversions with the conversion methods of TimeZoneInfo
.
If you want more accurate conversions, you might want to explore using Noda Time with IANA time zones instead of Windows time zones. (See the timezone tag wiki for details).
If you don't like the display names that .Net provides, or if you need localized time zone names, you can use the TimeZoneNames library instead of TimeZoneInfo.DisplayName
.
You also might consider simplifying the user experience, by having two dropdowns - one that would list out the countries of the world, and one that would choose a time zone within that country. That makes things very easy for countries with one time zone, and reduces the choices for users living in a country with multiple time zones.
And like I said, there are other approaches to consider, such as doing all UTC-to-local conversions in the browser itself, and only sending UTC to the server.
Regarding trying to get the user's time zone automatically via JavaScript, you should recognize that the approach in the CodeProject site you linked to is flawed because it mistakes a time zone for a time zone offset. (The timezone tag wiki covers more about this.) There are other ways to do client-side time zone detection, but they are imperfect. The best advice is to ask your user.
And finally, if you want to get really fancy with your UI, you could consider using a map-based time zone picker instead of (or in addition to) a drop down. The two I would consider are dosx timezone-picker, and timezonepicker.com.
If you have more specific concerns, consider searching for existing questions here on StackOverflow. There are many about time zones. You might also consider watching my video course, Date and Time Fundamentals on Pluralsight - which covers this and many other related topics.
Upvotes: 1