I have a welcome page that displays the user's account info. There is a time zone field where the user can select their time zone. For a better user experience, I guess the user's time zone from the browser and display that as the default value for the field. I guess the time zone by using Moment Timezone. More specifically, I call moment.tz.guess()
. For me, this returns "America/Los_Angeles". I pass the time zone the user selects (or the default time zone that the browser infers if they don't touch the time zone field) to the server, and the server creates the TimeZoneInfo
object for that user by calling TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
.
The issue is that "America/Los_Angeles" is not a time zone ID recognized by .NET, nor are any of the other time zone strings returned by moment-timezone. Here is a list of the time zone IDs recognized by .NET. So if the user sees the default time zone is correct for them and does not change it, then a time zone string that is not recognized by .NET is passed back to the server and cannot be parsed. The time zone field is a drop down menu with all the values coming from .NET, so if the user does select one of these time zones it will work fine.
Does anyone know a way around this? I can think of three general solutions right now:
1) Create a mapping between all possible values returned by moment.tz.guess()
to all time zone IDs. Not ideal, but if this exists already that would be great. I don't believe moment-timezone supports this itself.
2) Perhaps TimeZoneInfo
or some other .NET library has a way of creating a TimeZoneInfo
object using the info generated by moment-timezone.
3) Use a different method altogether of getting the user's time zone from the browser. One way of doing this is by doing some utc offset work.
The important thing is that this is just a guess -- it doesn't need to be 100% accurate because the user can select the correct time zone if it's wrong. I just want an easy way of guessing the user's time zone id so that it can be parsed server side.