如何将时间字段分解成小时、分钟和秒?

时间:2022-04-21 21:31:11

How would I parse my start_at column to be three different fields for hours, minutes and seconds instead of just one?

如何将start_at列解析为三个不同的字段,分别为小时、分钟和秒,而不是一个字段?

Note: I want to keep it as one column and not make three different ones.

注意:我想把它保持为一个列,而不是做三个不同的列。

Here is my code:

这是我的代码:

Table:

表:

class CreateTimers < ActiveRecord::Migration
  def change
    create_table :timers do |t|
      t.time :start_at

      t.timestamps
    end
  end
end

Form:

形式:

<%= form_for(@timer) do |f| %>

  <div class="form-inputs">
    <%= f.text_field :start_at %>
  </div>

  <div class="form-actions">
    <%= f.submit 'Start', :class => 'btn-primary span3' %>
  </div>
<% end %>

5 个解决方案

#1


2  

If you just want to split time to three separate inputs you can use the time_select helper.

如果只想将时间分割为三个独立的输入,可以使用time_select helper。

Otherwise use the strftime method; Check http://strfti.me for more help.

否则使用strftime方法;检查http://strfti。我更多的帮助。

#2


2  

You don't say what format your time values are in. Because users love to be different, if you give them a free-form text field, odds are really good you'll get data in varying formats, so you'll have to be flexible.

你没有说你的时间值是什么格式。因为用户喜欢与众不同,如果你给他们一个*格式的文本字段,很有可能你会得到不同格式的数据,所以你必须灵活。

You can try using DateTime's parse method, which provides some flexibility for the formats.

您可以尝试使用DateTime的解析方法,它为格式提供了一些灵活性。

DateTime.parse('2001-02-03T04:05:06+07:00')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>

DateTime.parse('20010203T040506+0700')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>

DateTime.parse('3rd Feb 2001 04:05:06 PM')
#=> #<DateTime: 2001-02-03T16:05:06+00:00 ...>

There's also the Chronic gem, which allows even more flexibility in how the values can be entered.

还有慢性gem,它允许更灵活地输入值。

Once you have a DateTime or Time value, you can use that object's methods to get at the hour, minute and second fields:

一旦您有了一个DateTime或Time值,您就可以使用该对象的方法来获取小时、分钟和秒字段:

now = Time.now # => 2012-10-03 07:50:26 -0700
now.hour       # => 7
now.min        # => 50
now.sec        # => 26

or:

或者:

require 'date'
now = DateTime.now # => #<DateTime: 2012-10-03T07:52:53-07:00 ((2456204j,53573s,622304000n),-25200s,2299161j)>
now.hour # => 7
now.min  # => 52
now.sec  # => 53

#3


1  

Check out strftime at http://www.ruby-doc.org/core-1.9.3/Time.html

查看strftime的http://www.ruby-doc.org/core-1.9.3/Time.html

#4


1  

For e.g.

如。

start_at - 2011-04-26 04:14:56 UTC

For hours:

几个小时:

start_at.strftime("%H") = "04" 

For minutes:

分钟:

start_at.strftime("%M") = "14"

For seconds:

秒:

start_at.strftime("%S") = "26"

#5


1  

start_at.strftime("%H %M %S")

More detailed documentation is at: http://www.ruby-doc.org/core-1.9.3/Time.html

更详细的文档参见:http://www.ruby-doc.org/core-1.9.3/Time.html

#1


2  

If you just want to split time to three separate inputs you can use the time_select helper.

如果只想将时间分割为三个独立的输入,可以使用time_select helper。

Otherwise use the strftime method; Check http://strfti.me for more help.

否则使用strftime方法;检查http://strfti。我更多的帮助。

#2


2  

You don't say what format your time values are in. Because users love to be different, if you give them a free-form text field, odds are really good you'll get data in varying formats, so you'll have to be flexible.

你没有说你的时间值是什么格式。因为用户喜欢与众不同,如果你给他们一个*格式的文本字段,很有可能你会得到不同格式的数据,所以你必须灵活。

You can try using DateTime's parse method, which provides some flexibility for the formats.

您可以尝试使用DateTime的解析方法,它为格式提供了一些灵活性。

DateTime.parse('2001-02-03T04:05:06+07:00')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>

DateTime.parse('20010203T040506+0700')
#=> #<DateTime: 2001-02-03T04:05:06+07:00 ...>

DateTime.parse('3rd Feb 2001 04:05:06 PM')
#=> #<DateTime: 2001-02-03T16:05:06+00:00 ...>

There's also the Chronic gem, which allows even more flexibility in how the values can be entered.

还有慢性gem,它允许更灵活地输入值。

Once you have a DateTime or Time value, you can use that object's methods to get at the hour, minute and second fields:

一旦您有了一个DateTime或Time值,您就可以使用该对象的方法来获取小时、分钟和秒字段:

now = Time.now # => 2012-10-03 07:50:26 -0700
now.hour       # => 7
now.min        # => 50
now.sec        # => 26

or:

或者:

require 'date'
now = DateTime.now # => #<DateTime: 2012-10-03T07:52:53-07:00 ((2456204j,53573s,622304000n),-25200s,2299161j)>
now.hour # => 7
now.min  # => 52
now.sec  # => 53

#3


1  

Check out strftime at http://www.ruby-doc.org/core-1.9.3/Time.html

查看strftime的http://www.ruby-doc.org/core-1.9.3/Time.html

#4


1  

For e.g.

如。

start_at - 2011-04-26 04:14:56 UTC

For hours:

几个小时:

start_at.strftime("%H") = "04" 

For minutes:

分钟:

start_at.strftime("%M") = "14"

For seconds:

秒:

start_at.strftime("%S") = "26"

#5


1  

start_at.strftime("%H %M %S")

More detailed documentation is at: http://www.ruby-doc.org/core-1.9.3/Time.html

更详细的文档参见:http://www.ruby-doc.org/core-1.9.3/Time.html