繁体   English   中英

如何使用Rails在浏览器的时区中显示DateTime?

[英]How to display a DateTime in the timezone of a browser using Rails?

Rails将所有DateTime存储在UTC时区中。 我需要在他们的浏览器报告他们所在的时区中向用户显示该时间。有一种简单的方法吗?

使用最新的Rails(3.2.13)。

有人把解决方案变成了一个宝石:

https://github.com/basecamp/local_time

在客户端(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 

信用: https//stackoverflow.com/a/5930596/643500

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM