如何使用 javascript 以 rails 形式获取本地时间? [英] How can I use javascript to get the local time in a rails form?

查看:82
本文介绍了如何使用 javascript 以 rails 形式获取本地时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 local_time gem 以用户本地时间显示日期,它适用于已添加的日期,以我的本地时间显示它们.

I'm using the local_time gem to show dates in users local times and it works well for dates already added, to display them in my local time.

我在添加新日期时遇到问题,我正在尝试通过 HTML 表单传递用户本地时间(以及日期).我认为客户端设备有他们的当地时间,所以我需要抓住它.

I'm having a problem adding a new date though and I am trying to pass the users local time (and thus date) in through the HTML form. I figure the client device has their local time and so I need to grab it.

现在我正在尝试向表单中添加一个字段,如下所示:

Right now I am trying to add a field to my form, as in:

= simple_form_for(@link)do |f|
...
= f.text_field :local_client_time, value: local_time("#{Time.now()}")

但那是显示

August 11, 2014 12:12pm" /> 

这不仅格式错误而且实际显示(应该隐藏),而且还显示UTC时间,比我的浏览器时间提前5小时(美国东部标准时间).

which is not only mal-formed and actually showing (should be hidden) but is also showing a utc time, 5 hours ahead of my browser time which is on EST.

如何在表单上传递用户本地时间?(可能是js)?

How can I pass in the users local time on the form? (probably js) ?

我也不知道如何在控制器上处理它.我目前有

I am also not sure how to process it on the controller. I currently have

@link = Link.new(params[:link])

以及之后的其他一些逻辑.我不确定在创建 Active Record 实例时如何让 rails 处理这个额外的参数(它会忽略它吗?)而且我也不确定如何使用通过隐藏字段传入的 local_client_timerails 可用于实际设置日期字段值的日期格式 verified_date

and a few other pieces of logic after that. I'm not sure how to let rails deal with this extra parameter when creating the Active Record instance (will it ignore it?) and I am also not sure how to then use the local_client_time that was passed in through a hidden field as in a date format that rails can use to actually set the value of a date field verified_date

注意:无论是否登录,我都需要为所有用户显示该字段,但这对于 local_time gem 来说似乎没问题.唯一的问题似乎是添加具有时间组件的日期字段.美国东部标准时间晚上 8 点之后,它会在第二天添加,然后显示会反映出来.另一个字段 ('content_date') 没有这些问题,但对于该字段,我只使用 f.text_field :content_date, id: 'datepicker', size: 10 并且在控制器中没有特殊处理和数据库中的日期字段.

Note: I will need to display the field for all users, whether logged in or not, but that seems ok with the local_time gem. The only issue seems to be with adding a date field that has a time component. After 8pm EST it gets added as the next day and then the display reflects that. Another field ('content_date') doesn't have these issues but for that field I just use f.text_field :content_date, id: 'datepicker', size: 10 and no special processing in the controller and a date field in the database.

两个日期字段看起来相似:

Both date fields look similar:

| content_date   | date         | YES  |     | NULL    |                |
| verified_date  | date         | YES  |     | NULL    |                |

我还想知道验证日期字段是否应该实际上是日期时间来解决这个问题.事情是我不关心时间组件,从不使用或显示它.应用程序的日期/时间字段的最低粒度级别为 1 天.

I am also wondering if the verified date field should actually be datetime instead to get around this. Thing is I don't care about the time component and never use or display it. The application's lowest level of granularity for date/time fields is 1 day.

推荐答案

不久前为我的应用解决了这个问题,用户时区有很多不同的实现,它确实让人难以理解并且难以测试.我会在底部发布链接,以便查看其他实现.

Solved this awhile back for my app, there are quite a few different implementations with user Timezones and it can get really way over one's head and daunting to test. I'll post links at bottom for places to see other implmntations.

对于您的直接请求,有一个很好的 gem 提供了 js 功能来检测客户端的时区.参考这个

For your direct request, there is a nice gem that provides the js functionality to detect client's timezone. Refer this

设置完成后,您可以执行以下操作:

Once setup, you can do something like:

$(function() {
  var detected_zone = Temporal.detect();
  $('#user_time_zone').val(detected_zone.timezone.name);
});

设置时区后,您可以使用 Time.zone.now 获取本地时间.

Once you have the timezone set, you can just use Time.zone.now to get the local time.

查看 Railscasts,了解如何跟踪数据库中每个用户的时区:链接

Have a look at the Railscasts on how to keep track of each individual user's timezones in the db: link

这篇关于如何使用 javascript 以 rails 形式获取本地时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆