MongoDB ObjectId to Timestamp Converter
Decode a MongoDB ObjectId into its creation time and its 12 bytes, turn a date into an ObjectId for _id range queries, or generate fresh ObjectIds.
Client-side only, nothing leaves your browser
Created
Byte layout
Get the timestamp in code
ObjectId("6566fe5c3d5e0f2a1b4c7d8e").getTimestamp()new ObjectId("6566fe5c3d5e0f2a1b4c7d8e").getTimestamp()ObjectId("6566fe5c3d5e0f2a1b4c7d8e").generation_timedb.collection.aggregate([{ $project: { createdAt: { $toDate: "$_id" } } }])How an ObjectId Is Built
Every ObjectId is 12 bytes, written as 24 hex characters. The first 4 bytes count the seconds since the Unix epoch, which is why you can read a creation time from any _id without a separate createdAt field. The next 5 bytes are a random value picked once per client process, and the last 3 bytes are a counter that starts at a random number and increments with each new id. The driver or mongosh builds the id on the client, so the time comes from the client's clock, not the server's.
Quick Reference
ObjectId("...").getTimestamp(): creation time as a Date in mongoshObjectId.createFromTime(seconds): Node.js, an ObjectId with zeros after the timestamp, for range queriesObjectId(seconds): mongosh, the integer replaces the timestamp but the rest stays randomObjectId(...).generation_time: Python bson, a timezone-aware UTC datetimeObjectId.from_datetime(dt): Python bson, for range queries only, never for inserts{ $toDate: "$_id" }: aggregation, the date stored in the ObjectId
Frequently asked questions
What is ObjectId in MongoDB?
MongoDB ObjectId vs UUID: which should I use?
Are MongoDB ObjectIds sortable by creation time?
Is a MongoDB ObjectId unique across collections?
How do I convert a MongoDB ObjectId to a string?
Need a MongoDB GUI Client?
1bench is a modern database client for MongoDB and 20+ other databases. Query, browse, and manage your data visually.
Learn More