3

I have a tool set to store everything in the db as UTC. On a form I have a text field with an attached jQuery Date + Time picker.

In that textfield the dateTime stamp (w/ UTC offset) will just appear like:

<%= f.text_field :report_time, :size => 22  %>
    2012-06-05 15:55:50

However I need this form choice to be in EST. So I need to convert it going to the form and then when the form is submitted. This problem is specific only to when an 'EDIT' action is taken (time already exists in field). If I do any addition or subtraction "UTC" gets stuck on the end in the form text field:

@report.report_time = @report.report_time - 5.hours
=>2012-06-05 10:55:50 UTC

At this point a jQuery DateJS dateparse function I'm using will stop working because it doesn't understand the "UTC" text in the text field holding the dateTime.

How can I take a db UTC time and add/subtract hours without "UTC" getting dropped in the form, or how can you remove the UTC offest information leaving only the raw dateTime information?

How do I:

@report.report_time = @report.created_at - 5.hours
=>2012-06-05 10:55:50 UTC
#  do something here to strip " UTC" off the dateTime   ??
=>2012-06-05 10:55:50

Thank You! Rails 2.3.5 / Ruby 1.8.7

4

1 に答える 1

1

デフォルトの表現が必要ない場合は、strftime 関数を使用してフィールドの値を直接設定します。

<%= f.text_field :report_time, :size => 22, :value =>"...." %>

http://ruby-doc.org/stdlib-1.8.7/libdoc/date/rdoc/DateTime.html#method-i-strftime

フォーム ビルダーで日付/時刻をより自然に使用する方法については、このリンクを参照してください。

http://guides.rubyonrails.org/form_helpers.html#using-date-and-time-form-helpers

于 2012-06-07T17:16:46.837 に答える