How can I use javascript to get the local time in

2019-08-07 05:57发布

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.

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()}")

but that is displaying

August 11, 2014 12:12pm" /> 

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.

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])

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

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    |                |

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.

2条回答
Ridiculous、
2楼-- · 2019-08-07 06:46

I'd think this would suffice:

= simple_form_for @link do |f|
...
  = f.text_field :local_client_time # This generates some unique ID I believe. Otherwise, define one yourself: id: <your-id>

# javascript
var localTime = new Date();
$('<ID generated through rails for your field>').val(localTime);

Rails will receive this additional parameter on the server side as long as it is part of the form. What you need to do is either add a parameter on your Active Record model class (for instance local_client_time), and then ensure that is allowed through your strong parameter filter (if you're using Rails 4).

If you have a field on your Active Record model, then Link.new(params[:link]) should do the trick, otherwise you need to define a method (def local_client_time=) on your model, which will then handle the input.

Surely there will be some Date management server side as well, but that shouldn't be too problematic.

查看更多
趁早两清
3楼-- · 2019-08-07 06:54

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.

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);
});

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

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

查看更多
登录 后发表回答