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
2010-10-22 05:24:33
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
2010-10-22 05:52:24
@Alexey, I updated my answer.
Jacob Relkin
2010-10-22 06:17:20
@Jacob, that looks like would work but what if the user had javascript turned off?
DJTripleThreat
2010-10-22 06:32:15
@DJTripleThreat, then there would be no way of doing it.
Jacob Relkin
2010-10-22 06:34:04
I thought that localtime could be extracted from request headers.
Alexey Zakharov
2010-10-22 07:32:21
@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
2010-10-23 04:37:48
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
2010-10-22 06:37:38