
[英]How to intercept timezone of a DateTime object and convert it to another timezone (Ruby/Rails)
[英]How to display a DateTime in the timezone of a browser using Rails?
Rails将所有DateTime
存储在UTC时区中。 我需要在他们的浏览器报告他们所在的时区中向用户显示该时间。有一种简单的方法吗?
使用最新的Rails(3.2.13)。
有人把解决方案变成了一个宝石:
在客户端(js):
function set_time_zone_offset() {
var current_time = new Date();
$.cookie('time_zone', current_time.getTimezoneOffset());
}
在应用控制器中:
before_filter :set_timezone
def set_timezone
min = request.cookies["time_zone"].to_i
Time.zone = ActiveSupport::TimeZone[-min.minutes]
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.