In this post i would like to describe how to detect user's timezone and auto set in dropdown. In C# there is class called TimeZoneInfo through which you can get system Timezone.
First of all get system timezone info and bind it to dropdown list.(In my case i am using mvc application that's why IEnumerable<SelectListItem> return type of object )
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7vLnxm8Y3WyVkailFpVrlDsL04BHkxxcQhqSHojyVStGZfZxHwePWPWFt5hdJpr-nMxSy4aieM_nWJWL41A3mN2r0TefdeR0zxhLPWd5KivayAWCYrWlCplbbYdj7nb6KkkPBNoi3M0zn/?imgmax=800)
Now to to auto select timezone as per user's location there is very simple solution is read offset value using date object using javacript as per below
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqy3uXZdxe2zyzky-2w54Cdxw7EMVNgYBVT17aWfAugYaBZLB0iMZAE4k-EFdXN71w1lWSus30qAqRQwrBpmvk5fmXAnKN7nWHPORKdQhaHpTVj_AglTzyhGmf4-BBgvZIvrRQ7_rp0M_J/?imgmax=800)
This offset will be in minutes and you need to store it somewhere (cookie is one option)
Now in above code in GetTimezone() function you need to read that value and set it selected value as per below.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhT2xLPEw0_3kyci-AdML3cfS9EUTESnSTSd3LWYKEvW659B9tWdC0BTA-eSilvdz7okuYs6Z4EAzexXjw0eLIrWI5tGizZwCE7Aud9fnVXJOSou8H06qwu8df62KVxFBQCYk47re6SQq54/?imgmax=800)
Here i haven't consider daylight savings time (DST), you can refer his link for more detail about that
![](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_tT1v6LvY7dZ4PfbYLJi7FUvZpc5fYEKp85CIJPtUeo4ts7hQghzwrwx66OCWTeSFangpTl7upT8_fdLCXCLqBq0duztNUvDKelvvM3WaucLWf0t37Z6ipeV_tteVzhjHsLHKejjETgxhnWhjU=s0-d)
First of all get system timezone info and bind it to dropdown list.(In my case i am using mvc application that's why IEnumerable<SelectListItem> return type of object )
Now to to auto select timezone as per user's location there is very simple solution is read offset value using date object using javacript as per below
This offset will be in minutes and you need to store it somewhere (cookie is one option)
Now in above code in GetTimezone() function you need to read that value and set it selected value as per below.
Here i haven't consider daylight savings time (DST), you can refer his link for more detail about that
No comments:
Post a Comment