数据映射器日期

发布于 2024-08-27 00:44:25 字数 317 浏览 4 评论 0原文

如果这是一个简单的答案,请原谅我。

但是如何从 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

Forgive me if this is a simple answer.

But how do you get a Date from a DataMapper property. For example:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒儿 2024-09-03 00:44:25

您可以使用以下功能访问它
http://ruby-doc.org/core/classes/DateTime.html

例如:

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.strftime(fmt='%F %T')
end

将返回格式为 YYYY-MM-DD HH:MM:SS 的日期,

这有帮助吗?

You can access it with the functions from
http://ruby-doc.org/core/classes/DateTime.html

For example:

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.strftime(fmt='%F %T')
end

will return a date formatted YYYY-MM-DD HH:MM:SS

Does that help?

负佳期 2024-09-03 00:44:25

或者确实

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

返回 ISO 8601 格式的日期,例如 2011-07-14T00:09:32+01:00

哦,而且没有必要指定 fmt=;但是

test.created_at.strftime("%F %T")

,如果您只想要日期,则可以

test.created_at.to_date.to_s

返回“2011-07-14”

另请记住,如果您只想存储日期而不是日期时间,则可以使用 created_on

Or indeed

test.created_at.to_time

returns a date such as 2011-07-14 00:09:32 +0100, including the offset.

Or

test.created_at.strftime("%c")

returns a date defined in the local format, such as Thu Jul 14 00:09:32 2011.

Or either of

test.created_at.iso8601
test.created_at.to_s

returns a date in ISO 8601 format, such as 2011-07-14T00:09:32+01:00.

Oh, and it isn't necessary to specify fmt=; you can do

test.created_at.strftime("%F %T")

However, if you just want the date, you can do

test.created_at.to_date.to_s

which returns "2011-07-14".

Bear in mind, also, that you can use created_on, if you only want to store a Date, and never a DateTime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文