1

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.

Drew
  • 1,277
  • 3
  • 21
  • 39
  • Thanks for pointing out the existing post -- I wasn't sure what the format returned by moment-timezone is called so I was having a hard time searching for that conversion – Drew May 23 '17 at 22:03
  • 1
    Convert with my library mentioned in the dup post if you are set on using `TimeZoneInfo`. But if you are just now building your app, I'd strongly suggest using [Noda Time](http://nodatime.org) instead, which works with IANA time zones directly. – Matt Johnson-Pint May 24 '17 at 15:58
  • Thanks for the advice. Since I only need to guess the user's time zone, I think I'm gonna do method (3) from my post by getting the summer and winter offset and then guessing the most likely time zone windows id on the backend. Ideally `TimeZoneInfo` will [support IANA time zones soon](https://github.com/dotnet/corefxlab/issues/338)! – Drew May 24 '17 at 17:44
  • From experience, I can tell you that approach is *really* complicated and error prone. Summer/Winter offsets alone are not enough to deduce the correct time zone in many cases. I strongly recommend against that. You're much better off taking the guess from moment-timezone and using my [TimeZoneConverter](https://github.com/mj1856/TimeZoneConverter) library if you are going to using `TimeZoneInfo`. – Matt Johnson-Pint May 24 '17 at 18:27
  • Cheers Matt. Again, I only need to guess the user's time zone to display a default time zone in a drop down menu. If the user sees that the incorrect time zone is displayed, they can simply choose the correct time zone from the drop down menu. I feel like moment-timezone and the TimeZoneConverter library, while great at figuring out the user's time zone, is a lot of overhead. – Drew May 24 '17 at 22:34
  • 1
    There's a lighter-weight version of logic similar to `moment.tz.guess()` in the [jsTimezoneDetect](https://bitbucket.org/pellepim/jstimezonedetect) library. – Matt Johnson-Pint May 24 '17 at 23:13

0 Answers0