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

时间:2018-03-03 20:42:53

标签: ruby-on-rails mongodb mongoid mongomapper

如何将MongoDB ID转换为各自的日期? (我对使用getTimestamp() ...)

的Javascript技术不感兴趣

1 个答案:

答案 0 :(得分: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"

为了我未来的自我...