Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 从单独的字符串创建日期和时间_Ruby - Fatal编程技术网

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>&nbsp;
      <%= link_to orientation.class_time, new_orientation_registration_path(orientation) %>
    <% else %>
      <li class="class-closed"><i class="fa fa-minus-circle"></i>&nbsp;<%= orientation.class_time 

你把它钉死了@tadman。为了子孙后代,这里是方法和调用

在我看来

    <% if orientation.active? and Orientation.is_not_past(orientation) %>
      <li class="class-available"><i class="fa fa-check"></i>&nbsp;
      <%= link_to orientation.class_time, new_orientation_registration_path(orientation) %>
    <% else %>
      <li class="class-closed"><i class="fa fa-minus-circle"></i>&nbsp;<%= 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答案的评论,还是您正在提交答案?如果是评论,请使用评论,不要创建答案。