我为我的班级创建了这个方法
def time_stamp(time)
time.strftime("%I:%M%p")
end
在我不需要时在 IRB 中,然后输入方法并输入
time_stamp(Time.now)
它返回:“07:57PM”这就是我想要的
在 Sinatra 中,虽然我创建了一个新的窥视对象:
@peep = Peep.new(:peep_timestamp => time_stamp(Time.now))
但是当我去 rackup 并查看我的本地时,它仍然有未格式化的时间:2015-01-17 19:15:23 +0000(例如)。我希望它说“07:57PM”或者我创建 Peep 对象的当前时间。
即使我打字
@peep6 = Peep.new(:peep_timestamp => "8:34PM")
它返回:
<Peep @id=nil @message=nil @peep_timestamp=2015-01-17 20:34:00 +0000>
我的整个窥视课看起来像:
class Peep
include DataMapper::Resource
property :id, Serial
property :message, Text
property :peep_timestamp, Time
property :username, String
def time_stamp(time)
time.strftime("%I:%M%p")
end
end