Getting timestamp from mongodb id

前端 未结 8 1785
误落风尘
误落风尘 2020-12-02 08:38

How do I get the timestamp from the MongoDB id?

8条回答
  •  感情败类
    2020-12-02 09:07

    The timestamp is contained in the first 4 bytes of a mongoDB id (see: http://www.mongodb.org/display/DOCS/Object+IDs).

    So your timestamp is:

    timestamp = _id.toString().substring(0,8)
    

    and

    date = new Date( parseInt( timestamp, 16 ) * 1000 )
    

提交回复
热议问题