gpt4 book ai didi

ruby-on-rails - Rails 如何从 dateTime 中删除 UTC 偏移文本 ('UTC' ) 只留下 DateTime?

转载 作者:行者123 更新时间:2023-12-03 08:30:15 24 4
gpt4 key购买 nike

我有一个工具集可以将数据库中的所有内容存储为 UTC。在一个表单上,我有一个带有附加 jQuery 日期 + 时间选择器的文本字段。

在该文本字段中,日期时间戳(带 UTC 偏移量)将显示为:

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

但是我需要这个表格选择在 EST 中。所以我需要将它转换为表单,然后在提交表单时。此问题仅特定于执行“编辑”操作时(字段中已存在时间)。如果我做任何加法或减法,“UTC”会卡在表单文本字段的末尾:

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

此时,我正在使用的 jQuery DateJS 日期解析函数将停止工作,因为它不理解保存日期时间的文本字段中的“UTC”文本。

我如何获取数据库 UTC 时间并添加/减去小时数而不在表单中删除“UTC”,或者如何删除 UTC offest 信息只留下原始日期时间信息?

我如何:

@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

谢谢!Rails 2.3.5/Ruby 1.8.7

最佳答案

如果您不想要默认表示,请使用 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

关于ruby-on-rails - Rails 如何从 dateTime 中删除 UTC 偏移文本 ('UTC' ) 只留下 DateTime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10936319/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com