有效的 Model
DateTIme 類型
表示日期和時間的屬性類型。它註冊在 :datetime
鍵下。
class Event
include ActiveModel::Attributes
attribute :start, :datetime
end
event = Event.new
event.start = "Wed, 04 Sep 2013 03:00:00 EAT"
event.start.class # => Time
event.start.year # => 2013
event.start.month # => 9
event.start.day # => 4
event.start.hour # => 3
event.start.min # => 0
event.start.sec # => 0
event.start.zone # => "EAT"
字串
值使用 ISO 8601 日期時間格式來解析。也接受不完整僅限時間的格式。
event.start = "06:07:08+09:00"
event.start.utc # => 1999-12-31 21:07:08 UTC
在宣告屬性時可以自訂次秒精準度。
class Event
include ActiveModel::Attributes
attribute :start, :datetime, precision: 4
end
方法
- T
包含的模組
實例公共方法
type() 連結
來源: 顯示 | 在 GitHub 上
# File activemodel/lib/active_model/type/date_time.rb, line 49 def type :datetime end