如果这是一个简单的答案,请原谅我。
但是如何从DataMapper属性中获取日期呢?例如:
require 'rubygems'
require 'sinatra'
require 'datamapper'
class Test
include DataMapper::Resource
property :id, Serial
property :created_at, Date
end
get '/:id' do
test = Test.get(1)
test.created_at = ?
end发布于 2011-07-14 07:17:41
或者确实如此
test.created_at.to_time返回日期,如2011-07-14 00:09:32 +0100,包括偏移量。
或
test.created_at.strftime("%c")返回以本地格式定义的日期,如Thu Jul 14 00:09:32 2011。
或以下任一项
test.created_at.iso8601
test.created_at.to_s返回ISO8601格式的日期,如2011-07-14T00:09:32+01:00。
哦,并且没有必要指定fmt=;您可以这样做
test.created_at.strftime("%F %T")但是,如果您只是想要日期,您可以这样做
test.created_at.to_date.to_s它返回"2011-07-14"。
还请记住,如果您只想存储日期,而不是DateTime,则可以使用created_on。
https://stackoverflow.com/questions/2549473
复制相似问题