I want to find a users time zone from his ip address. If the user is travelling the application should be able to find out the current time zone in which he is.
Asked
Active
Viewed 4,182 times
2 Answers
2
You can't, at least reliably: think of proxies, ISP NAT or VPNs, even if a user seems in a country it doesn't mean he's really there.
I would suggest to let the user choose his preferred time zone, and let him alter it if he needs to.
Otherwise, have a look at this: Determine a User's Timezone
-
If a user is using proxies or other stuff its his problem :). – Sachin Prasad Nov 19 '12 at 08:03
-
@Sachin Prasad: It depends on what *you* are doing with that information. If you're using it to make decisions, you can end in a world of hurt. – Albireo Nov 20 '12 at 14:58
-
I agree with Sachin. I'm just trying to use it to for display or order receipt. I can display the timezone with the time, but my users can't seem to figure out how to subtract 10. I offered my boss a calculator for the math. He thought I was being sarcastic. :) – baash05 Dec 04 '12 at 05:34
-
1if its just for formatting, use https://bitbucket.org/pellepim/jstimezonedetect to detect and something like moment.js to format in the browser itself. If your app is going to do something in the server with the time zone, like sending an email with a formated date, then http://thisbythem.com/blog/clientside-timezone-detection/ – kisai Mar 31 '15 at 19:44
-
And of course detection of the tz for the server is only useful as a custom default, the user has the last word about it. – kisai Mar 31 '15 at 19:52
1
Yes, you can get the timezone in rails based on the user's IP address but it won't be accurate always because of proxies or VPNs.
Why?
- Your application will get only the latest IP(think ISP provider and VPN) from the request header and you can find timezone based on it.
However, if you have a chance to overlook this shortcoming you can use check this gem with the IP lookup service. To test, you can use the ipapi_com service(it is free up to 45/minute). For the implementation hints, you can have a look here.

Tamal Chakroborty
- 71
- 3