将MongoDB ObjectID转换为Rails中的时间戳

问题描述 投票:0回答:1

如何将MongoDB ID转换为各自的日期? (我对使用getTimestamp()的Javascript技术不感兴趣...)

ruby-on-rails mongodb mongoid mongomapper
1个回答
1
投票

如果您没有在MongoMapper或Mongoid中使用timestamps!,您仍然可以从ObjectID本身获取created_at日期:

user = User.last
user.id
# => BSON::ObjectId('5a90731ddcd2d5008c000443')
user.id.generation_time.strftime('%Y-%m-%d %H:%M')
# => "2018-02-23 20:01"

为了我未来的自我......

© www.soinside.com 2019 - 2024. All rights reserved.