Ruby 从单独的字符串创建日期和时间
我有一个方向模型,看起来像这样:Ruby 从单独的字符串创建日期和时间,ruby,Ruby,我有一个方向模型,看起来像这样: create_table "orientations", :force => true do |t| t.date "class_date" t.text "class_time" t.integer "seats" t.boolean "active", :default => true t.datetime "created_at", :n
create_table "orientations", :force => true do |t|
t.date "class_date"
t.text "class_time"
t.integer "seats"
t.boolean "active", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
date = '2014-07-09'
time = '9:30am'
DateTime.parse([ date, time ].join(' '))
# => Wed, 09 Jul 2014 09:30:00 +0000
我想将class\u date
和class\u time
字段连接起来,创建一个可以与当前日期/时间进行比较的DateTime对象。我知道我可以使用以下方法解析时间:
1.9.3-p545 :022 > o.class_time
=> "9:30am"
1.9.3-p545 :023 > Time.parse(o.class_time)
=> 2014-07-28 09:30:00 -0400
但我不知道如何将其与上课日期结合起来。您可以将日期和时间结合起来,如下所示:
create_table "orientations", :force => true do |t|
t.date "class_date"
t.text "class_time"
t.integer "seats"
t.boolean "active", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
date = '2014-07-09'
time = '9:30am'
DateTime.parse([ date, time ].join(' '))
# => Wed, 09 Jul 2014 09:30:00 +0000
我已将Rails中的本地时区设置为UTC。您可以将日期和时间组合为如下:
create_table "orientations", :force => true do |t|
t.date "class_date"
t.text "class_time"
t.integer "seats"
t.boolean "active", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
date = '2014-07-09'
time = '9:30am'
DateTime.parse([ date, time ].join(' '))
# => Wed, 09 Jul 2014 09:30:00 +0000
我已将Rails中的本地时区设置为UTC。对于
日期
类,有类似的类方法parse
:
pry(main)> Date.parse '2013-08-31'
=> #<Date: 2013-08-31 ((2456536j,0s,0n),+0s,2299161j)>
您可以在我的文章中了解更多关于Ruby中日期/时间的信息对于
date
类,有类似的类方法parse
:
pry(main)> Date.parse '2013-08-31'
=> #<Date: 2013-08-31 ((2456536j,0s,0n),+0s,2299161j)>
在我的文章中,你可以在Ruby中了解更多关于日期/时间的信息。为了子孙后代,这里是方法和调用 在我看来
<% if orientation.active? and Orientation.is_not_past(orientation) %>
<li class="class-available"><i class="fa fa-check"></i>
<%= link_to orientation.class_time, new_orientation_registration_path(orientation) %>
<% else %>
<li class="class-closed"><i class="fa fa-minus-circle"></i> <%= orientation.class_time
你把它钉死了@tadman。为了子孙后代,这里是方法和调用 在我看来
<% if orientation.active? and Orientation.is_not_past(orientation) %>
<li class="class-available"><i class="fa fa-check"></i>
<%= link_to orientation.class_time, new_orientation_registration_path(orientation) %>
<% else %>
<li class="class-closed"><i class="fa fa-minus-circle"></i> <%= orientation.class_time
您是否尝试过
DateTime.parse(“{o.class\u date}{o.class\u time}”)
?是否有任何理由不在表中使用DateTime
类型?是否尝试过DateTime.parse({o.class\u date}{o.class\u time}”)
?您没有在表中键入日期时间的任何原因?这是对@tadman答案的评论,还是您正在提交答案?如果是评论,那么请使用评论,不要创建答案。这是对@tadman答案的评论,还是您正在提交答案?如果是评论,请使用评论,不要创建答案。