views:

39

answers:

2

Does rails provide solution to get website user time and timezone configured on his local machine, which is used in his web browser?

+1  A: 

Rails (Ruby actually) is a server-side language, so to retrieve data about the client side you would need to use JavaScript:

var currentTime = new Date();
var zone = -(currentTime.getTimezoneOffset() / 60); //GMT
Jacob Relkin
You haven't understand me. Time class returns time on the server machine. I want to get current time on clinet machine which browser the website.
Alexey Zakharov
@Alexey, I updated my answer.
Jacob Relkin
@Jacob, that looks like would work but what if the user had javascript turned off?
DJTripleThreat
@DJTripleThreat, then there would be no way of doing it.
Jacob Relkin
I thought that localtime could be extracted from request headers.
Alexey Zakharov
@Alexey, that would be great if it did. I'd also like browsers to send whether a browser had javascript turned on or off. Sadly, neither of these are true.
DJTripleThreat
A: 

Jacob's answer will work if the user has javascript turned on. What you might try doing is allowing the user to select their time zone. There is a select_tag that can be used in a form called the time_zone_select and time_zone_options_for_select

See: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-time_zone_options_for_select And: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-time_zone_select

DJTripleThreat